Skip to content

Commit 31ef684

Browse files
arobase-chewooorm
andcommitted
remark: add support for meta in code
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>
1 parent 9c5e2ed commit 31ef684

File tree

93 files changed

+1322
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1322
-4
lines changed

packages/remark-parse/lib/tokenize/code-fenced.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ function fencedCode(eat, value, silent) {
2323
var marker;
2424
var character;
2525
var flag;
26+
var lang;
27+
var meta;
2628
var queue;
2729
var content;
2830
var exdentedContent;
@@ -228,9 +230,26 @@ function fencedCode(eat, value, silent) {
228230

229231
subvalue += content + closing;
230232

233+
index = -1;
234+
length = flag.length;
235+
236+
while (++index < length) {
237+
character = flag.charAt(index);
238+
239+
if (character === C_SPACE || character === C_TAB) {
240+
if (!lang) {
241+
lang = flag.slice(0, index);
242+
}
243+
} else if (lang) {
244+
meta = flag.slice(index);
245+
break;
246+
}
247+
}
248+
231249
return eat(subvalue)({
232250
type: 'code',
233-
lang: flag || null,
251+
lang: lang || flag || null,
252+
meta: meta || null,
234253
value: trim(exdentedContent)
235254
});
236255
}

packages/remark-parse/lib/tokenize/code-indented.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function indentedCode(eat, value, silent) {
9292
return eat(subvalue)({
9393
type: 'code',
9494
lang: null,
95+
meta: null,
9596
value: trim(content)
9697
});
9798
}

packages/remark-stringify/lib/visitors/code.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ function code(node, parent) {
3838
var value = node.value;
3939
var options = self.options;
4040
var marker = options.fence;
41-
var language = self.encode(node.lang || '', node);
41+
var info = node.lang || '';
4242
var fence;
4343

44+
if (info && node.meta) {
45+
info += ' ' + node.meta;
46+
}
47+
48+
info = self.encode(info, node);
49+
4450
/* Without (needed) fences. */
45-
if (!language && !options.fences && value) {
51+
if (!info && !options.fences && value) {
4652
/* Throw when pedantic, in a list item which
4753
* isn’t compiled using a tab. */
4854
if (
@@ -59,5 +65,5 @@ function code(node, parent) {
5965

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

62-
return fence + language + '\n' + value + '\n' + fence;
68+
return fence + info + '\n' + value + '\n' + fence;
6369
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Normal with language tag:
2+
3+
```js this is an info string
4+
```
5+
6+
With white space:
7+
8+
``` bash info
9+
```
10+
11+
With curly braces:
12+
13+
``````lang{info}
14+
``````
15+
16+
With nothing:
17+
18+
```
19+
```
20+
21+
With several streaks of white space:
22+
23+
``` bash info string
24+
```
25+
26+
With tabs as white space:
27+
28+
``` bash info
29+
```
30+
31+
With spaces as white space:
32+
33+
``` bash info
34+
```

test/fixtures/tree/auto-link.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@
694694
{
695695
"type": "code",
696696
"lang": null,
697+
"meta": null,
697698
"value": "or here: <http://example.com/>",
698699
"position": {
699700
"start": {

test/fixtures/tree/backslash-escapes.commonmark.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,7 @@
19161916
{
19171917
"type": "code",
19181918
"lang": null,
1919+
"meta": null,
19191920
"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: \\-",
19201921
"position": {
19211922
"start": {
@@ -2018,6 +2019,7 @@
20182019
{
20192020
"type": "code",
20202021
"lang": null,
2022+
"meta": null,
20212023
"value": "Pipe: \\|\n\nTilde: \\~",
20222024
"position": {
20232025
"start": {
@@ -2092,6 +2094,7 @@
20922094
{
20932095
"type": "code",
20942096
"lang": null,
2097+
"meta": null,
20952098
"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.",
20962099
"position": {
20972100
"start": {

test/fixtures/tree/backslash-escapes.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,7 @@
16451645
{
16461646
"type": "code",
16471647
"lang": null,
1648+
"meta": null,
16481649
"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: \\-",
16491650
"position": {
16501651
"start": {
@@ -1747,6 +1748,7 @@
17471748
{
17481749
"type": "code",
17491750
"lang": null,
1751+
"meta": null,
17501752
"value": "Pipe: \\|\n\nTilde: \\~",
17511753
"position": {
17521754
"start": {
@@ -1821,6 +1823,7 @@
18211823
{
18221824
"type": "code",
18231825
"lang": null,
1826+
"meta": null,
18241827
"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.",
18251828
"position": {
18261829
"start": {

test/fixtures/tree/backslash-escapes.nogfm.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,7 @@
16111611
{
16121612
"type": "code",
16131613
"lang": null,
1614+
"meta": null,
16141615
"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: \\-",
16151616
"position": {
16161617
"start": {
@@ -1713,6 +1714,7 @@
17131714
{
17141715
"type": "code",
17151716
"lang": null,
1717+
"meta": null,
17161718
"value": "Pipe: \\|\n\nTilde: \\~",
17171719
"position": {
17181720
"start": {
@@ -1787,6 +1789,7 @@
17871789
{
17881790
"type": "code",
17891791
"lang": null,
1792+
"meta": null,
17901793
"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.",
17911794
"position": {
17921795
"start": {

test/fixtures/tree/block-elements.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@
496496
{
497497
"type": "code",
498498
"lang": null,
499+
"meta": null,
499500
"value": "1. Second sublist.",
500501
"position": {
501502
"start": {
@@ -625,6 +626,7 @@
625626
{
626627
"type": "code",
627628
"lang": null,
629+
"meta": null,
628630
"value": "And this is code();",
629631
"position": {
630632
"start": {

test/fixtures/tree/blockquote-lazy-code.commonmark.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{
88
"type": "code",
99
"lang": null,
10+
"meta": null,
1011
"value": "foo",
1112
"position": {
1213
"start": {
@@ -40,6 +41,7 @@
4041
{
4142
"type": "code",
4243
"lang": null,
44+
"meta": null,
4345
"value": "bar",
4446
"position": {
4547
"start": {

0 commit comments

Comments
 (0)