Skip to content

Commit

Permalink
remark: add support for meta in code
Browse files Browse the repository at this point in the history
Previously, the info string of a code block, was not parsed at all, and was stored in `lang`
on `code` nodes.
Now, the first “word” of the info string is stored in `lang`, whereas the rest is stored in
`meta`.

Related to syntax-tree/mdast#22 (PR)
Closes GH-342 (issue)
Closes GH-345 (PR)

Reviewed-by: Titus Wormer <tituswormer@gmail.com> 
Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> 

Co-authored-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
arobase-che and wooorm committed Oct 6, 2018
1 parent 9c5e2ed commit 31ef684
Show file tree
Hide file tree
Showing 93 changed files with 1,322 additions and 4 deletions.
21 changes: 20 additions & 1 deletion packages/remark-parse/lib/tokenize/code-fenced.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function fencedCode(eat, value, silent) {
var marker;
var character;
var flag;
var lang;
var meta;
var queue;
var content;
var exdentedContent;
Expand Down Expand Up @@ -228,9 +230,26 @@ function fencedCode(eat, value, silent) {

subvalue += content + closing;

index = -1;
length = flag.length;

while (++index < length) {
character = flag.charAt(index);

if (character === C_SPACE || character === C_TAB) {
if (!lang) {
lang = flag.slice(0, index);
}
} else if (lang) {
meta = flag.slice(index);
break;
}
}

return eat(subvalue)({
type: 'code',
lang: flag || null,
lang: lang || flag || null,
meta: meta || null,
value: trim(exdentedContent)
});
}
1 change: 1 addition & 0 deletions packages/remark-parse/lib/tokenize/code-indented.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function indentedCode(eat, value, silent) {
return eat(subvalue)({
type: 'code',
lang: null,
meta: null,
value: trim(content)
});
}
Expand Down
12 changes: 9 additions & 3 deletions packages/remark-stringify/lib/visitors/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ function code(node, parent) {
var value = node.value;
var options = self.options;
var marker = options.fence;
var language = self.encode(node.lang || '', node);
var info = node.lang || '';
var fence;

if (info && node.meta) {
info += ' ' + node.meta;
}

info = self.encode(info, node);

/* Without (needed) fences. */
if (!language && !options.fences && value) {
if (!info && !options.fences && value) {
/* Throw when pedantic, in a list item which
* isn’t compiled using a tab. */
if (
Expand All @@ -59,5 +65,5 @@ function code(node, parent) {

fence = repeat(marker, Math.max(streak(value, marker) + 1, 3));

return fence + language + '\n' + value + '\n' + fence;
return fence + info + '\n' + value + '\n' + fence;
}
34 changes: 34 additions & 0 deletions test/fixtures/input/fenced-code-info-string.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Normal with language tag:

```js this is an info string
```

With white space:

``` bash info
```

With curly braces:

``````lang{info}
``````

With nothing:

```
```

With several streaks of white space:

``` bash info string
```

With tabs as white space:

``` bash info
```

With spaces as white space:

``` bash info
```
1 change: 1 addition & 0 deletions test/fixtures/tree/auto-link.json
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "or here: <http://example.com/>",
"position": {
"start": {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/tree/backslash-escapes.commonmark.json
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "Backslash: \\\\\n\nBacktick: \\`\n\nAsterisk: \\*\n\nUnderscore: \\_\n\nLeft brace: \\{\n\nRight brace: \\}\n\nLeft bracket: \\[\n\nRight bracket: \\]\n\nLeft paren: \\(\n\nRight paren: \\)\n\nGreater-than: \\>\n\nHash: \\#\n\nPeriod: \\.\n\nBang: \\!\n\nPlus: \\+\n\nMinus: \\-",
"position": {
"start": {
Expand Down Expand Up @@ -2018,6 +2019,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "Pipe: \\|\n\nTilde: \\~",
"position": {
"start": {
Expand Down Expand Up @@ -2092,6 +2094,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "Quote: \\\"\n\nDollar: \\$\n\nPercentage: \\%\n\nAmpersand: \\&\n\nSingle quote: \\'\n\nComma: \\,\n\nForward slash: \\/\n\nColon: \\:\n\nSemicolon: \\;\n\nLess-than: \\<\n\nEquals: \\=\n\nQuestion mark: \\?\n\nAt-sign: \\@\n\nCaret: \\^\n\nNew line: \\\nonly works in paragraphs.",
"position": {
"start": {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/tree/backslash-escapes.json
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "Backslash: \\\\\n\nBacktick: \\`\n\nAsterisk: \\*\n\nUnderscore: \\_\n\nLeft brace: \\{\n\nRight brace: \\}\n\nLeft bracket: \\[\n\nRight bracket: \\]\n\nLeft paren: \\(\n\nRight paren: \\)\n\nGreater-than: \\>\n\nHash: \\#\n\nPeriod: \\.\n\nBang: \\!\n\nPlus: \\+\n\nMinus: \\-",
"position": {
"start": {
Expand Down Expand Up @@ -1747,6 +1748,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "Pipe: \\|\n\nTilde: \\~",
"position": {
"start": {
Expand Down Expand Up @@ -1821,6 +1823,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "Quote: \\\"\n\nDollar: \\$\n\nPercentage: \\%\n\nAmpersand: \\&\n\nSingle quote: \\'\n\nComma: \\,\n\nForward slash: \\/\n\nColon: \\:\n\nSemicolon: \\;\n\nLess-than: \\<\n\nEquals: \\=\n\nQuestion mark: \\?\n\nAt-sign: \\@\n\nCaret: \\^\n\nNew line: \\\nonly works in paragraphs.",
"position": {
"start": {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/tree/backslash-escapes.nogfm.json
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "Backslash: \\\\\n\nBacktick: \\`\n\nAsterisk: \\*\n\nUnderscore: \\_\n\nLeft brace: \\{\n\nRight brace: \\}\n\nLeft bracket: \\[\n\nRight bracket: \\]\n\nLeft paren: \\(\n\nRight paren: \\)\n\nGreater-than: \\>\n\nHash: \\#\n\nPeriod: \\.\n\nBang: \\!\n\nPlus: \\+\n\nMinus: \\-",
"position": {
"start": {
Expand Down Expand Up @@ -1713,6 +1714,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "Pipe: \\|\n\nTilde: \\~",
"position": {
"start": {
Expand Down Expand Up @@ -1787,6 +1789,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "Quote: \\\"\n\nDollar: \\$\n\nPercentage: \\%\n\nAmpersand: \\&\n\nSingle quote: \\'\n\nComma: \\,\n\nForward slash: \\/\n\nColon: \\:\n\nSemicolon: \\;\n\nLess-than: \\<\n\nEquals: \\=\n\nQuestion mark: \\?\n\nAt-sign: \\@\n\nCaret: \\^\n\nNew line: \\\nonly works in paragraphs.",
"position": {
"start": {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/tree/block-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "1. Second sublist.",
"position": {
"start": {
Expand Down Expand Up @@ -625,6 +626,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "And this is code();",
"position": {
"start": {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/tree/blockquote-lazy-code.commonmark.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "foo",
"position": {
"start": {
Expand Down Expand Up @@ -40,6 +41,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "bar",
"position": {
"start": {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/tree/blockquote-lazy-code.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "foo\nbar",
"position": {
"start": {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/tree/blockquote-lazy-fence.commonmark.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "aNormalCodeBlockInABlockqoute();",
"position": {
"start": {
Expand Down Expand Up @@ -84,6 +85,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "thisIsAlsoSomeCodeInABlockquote();",
"position": {
"start": {
Expand Down Expand Up @@ -161,6 +163,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "aNonTerminatedCodeBlockInABlockquote();",
"position": {
"start": {
Expand Down Expand Up @@ -198,6 +201,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "aNewCodeBlockFollowingTheBlockQuote();",
"position": {
"start": {
Expand Down Expand Up @@ -307,6 +311,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "aNewCodeBlock();",
"position": {
"start": {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/tree/blockquote-lazy-fence.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "aNormalCodeBlockInABlockqoute();",
"position": {
"start": {
Expand Down Expand Up @@ -84,6 +85,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "thisIsAlsoSomeCodeInABlockquote();",
"position": {
"start": {
Expand Down Expand Up @@ -161,6 +163,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "aNonTerminatedCodeBlockInABlockquote();",
"position": {
"start": {
Expand Down Expand Up @@ -217,6 +220,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "",
"position": {
"start": {
Expand Down Expand Up @@ -328,6 +332,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "aNewCodeBlock();",
"position": {
"start": {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/tree/blockquotes-with-code-blocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "sub status {\n print \"working\";\n}",
"position": {
"start": {
Expand Down Expand Up @@ -98,6 +99,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "sub status {\n return \"working\";\n}",
"position": {
"start": {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/tree/code-block-indentation.nooutput.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": " This is a code block...\n \n ...which is not exdented.",
"position": {
"start": {
Expand Down Expand Up @@ -103,6 +104,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": " This one...\n \n ...is.",
"position": {
"start": {
Expand Down Expand Up @@ -161,6 +163,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "So is this...\n \n ...one.",
"position": {
"start": {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/tree/code-block-indentation.nooutput.nogfm.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "...which is not exdented.",
"position": {
"start": {
Expand Down Expand Up @@ -296,6 +297,7 @@
{
"type": "code",
"lang": null,
"meta": null,
"value": "...is.",
"position": {
"start": {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/tree/code-block-nesting-bug.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
{
"type": "code",
"lang": "foo",
"meta": null,
"value": "```bar\nbaz\n```",
"position": {
"start": {
Expand Down Expand Up @@ -225,6 +226,7 @@
{
"type": "code",
"lang": "foo",
"meta": null,
"value": "~~~bar\nbaz\n~~~",
"position": {
"start": {
Expand Down Expand Up @@ -283,6 +285,7 @@
{
"type": "code",
"lang": "foo",
"meta": null,
"value": "~~~bar\nbaz\n~~~",
"position": {
"start": {
Expand All @@ -306,6 +309,7 @@
{
"type": "code",
"lang": "foo",
"meta": null,
"value": "```bar\nbaz\n```",
"position": {
"start": {
Expand Down
Loading

0 comments on commit 31ef684

Please # to comment.