Skip to content

Commit 6b5bc25

Browse files
fix: add gfm format for remark fixed #1374 (#1375)
Plugins for remark was reworks because @link should be catch by first then autolink
1 parent 00d434f commit 6b5bc25

File tree

9 files changed

+301
-33
lines changed

9 files changed

+301
-33
lines changed

Diff for: __tests__/__snapshots__/test.js.snap

+80
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,16 @@ exports[`html nested.input.js 1`] = `
12121212

12131213
</li>
12141214

1215+
1216+
<li><a
1217+
href='#tableobj'
1218+
class=\\"\\">
1219+
tableObj
1220+
1221+
</a>
1222+
1223+
</li>
1224+
12151225
</ul>
12161226
</div>
12171227
<div class='mt1 h6 quiet'>
@@ -2451,6 +2461,76 @@ like a <a href=\\"#klass\\">klass</a>. This needs a <a href=\\"https://developer
24512461
</div>
24522462

24532463
</div>
2464+
2465+
2466+
2467+
2468+
2469+
2470+
2471+
2472+
</section>
2473+
2474+
2475+
2476+
2477+
<section class='p2 mb2 clearfix bg-white minishadow'>
2478+
2479+
2480+
<div class='clearfix'>
2481+
2482+
<h3 class='fl m0' id='tableobj'>
2483+
tableObj
2484+
</h3>
2485+
2486+
2487+
</div>
2488+
2489+
2490+
<table>
2491+
<thead>
2492+
<tr>
2493+
<th>Col 1</th>
2494+
<th>Col 2</th>
2495+
<th>Col 3</th>
2496+
</tr>
2497+
</thead>
2498+
<tbody>
2499+
<tr>
2500+
<td>Dat 1</td>
2501+
<td>Dat 2</td>
2502+
<td>Dat 3</td>
2503+
</tr>
2504+
<tr>
2505+
<td>Dat 4</td>
2506+
<td>Dat 5</td>
2507+
<td>Dat 6</td>
2508+
</tr>
2509+
</tbody>
2510+
</table>
2511+
2512+
<div class='pre p1 fill-light mt0'>tableObj</div>
2513+
2514+
2515+
2516+
2517+
2518+
2519+
2520+
2521+
2522+
2523+
2524+
2525+
2526+
2527+
2528+
2529+
2530+
2531+
2532+
2533+
24542534

24552535

24562536

Diff for: __tests__/fixture/html/nested.input.js

+8
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,11 @@ var customStreams = {};
158158
customStreams.passthrough = function() {
159159
this.custom = true;
160160
};
161+
162+
/**
163+
* | Col 1 | Col 2 | Col 3 |
164+
* |-------|-------|-------|
165+
* | Dat 1 | Dat 2 | Dat 3 |
166+
* | Dat 4 | Dat 5 | Dat 6 |
167+
*/
168+
var tableObj = {};

Diff for: __tests__/lib/parse.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
const parse = require('../../src/parsers/javascript');
2-
const remarkP = require('remark')().parse;
3-
const removePosition = require('../../src/remark-remove-position')();
2+
const removePosition = require('../../src/remark-remove-position');
3+
const remarkParse = require('remark')().use(removePosition).parse;
44
const visit = require('unist-util-visit');
5-
const remarkParse = function (str) {
6-
return removePosition(remarkP(str));
7-
};
85

96
function pick(obj, props) {
107
if (Array.isArray(props)) {

Diff for: __tests__/lib/parsers/javascript.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
const remarkP = require('remark')().parse;
2-
const removePosition = require('../../../src/remark-remove-position')();
1+
const removePosition = require('../../../src/remark-remove-position');
2+
const remarkParse = require('remark')().use(removePosition).parse;
33
const parse = require('../../../src/parsers/javascript');
4-
const remarkParse = function (str) {
5-
return removePosition(remarkP(str));
6-
};
74

85
function toComments(source, filename, opts) {
96
source = typeof source === 'string' ? source : '(' + source.toString() + ')';

Diff for: package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"pify": "^5.0.0",
3939
"read-pkg-up": "^4.0.0",
4040
"remark": "^13.0.0",
41+
"remark-gfm": "^1.0.0",
4142
"remark-html": "^13.0.1",
4243
"remark-reference-links": "^5.0.0",
4344
"remark-toc": "^7.2.0",
@@ -55,8 +56,8 @@
5556
"yargs": "^15.3.1"
5657
},
5758
"optionalDependencies": {
58-
"vue-template-compiler": "^2.6.12",
59-
"@vue/compiler-sfc": "^3.0.11"
59+
"@vue/compiler-sfc": "^3.0.11",
60+
"vue-template-compiler": "^2.6.12"
6061
},
6162
"devDependencies": {
6263
"chdir": "0.0.0",

Diff for: src/remark-jsDoc-link.js

+85-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,55 @@
11
const findAndReplace = require('mdast-util-find-and-replace');
2+
const markdownLineEnding = require('micromark/dist/character/markdown-line-ending');
3+
4+
const link = '@link';
5+
const tutorial = '@tutorial';
6+
7+
function tokenizeJsDoclink(effects, ok, nok) {
8+
let index = 0;
9+
let focus = link;
10+
11+
function atext(code) {
12+
if (index !== link.length) {
13+
if (focus.charCodeAt(index) === code) {
14+
effects.consume(code);
15+
index++;
16+
return atext;
17+
} else if (tutorial.charCodeAt(index) === code) {
18+
focus = tutorial;
19+
}
20+
return nok(code);
21+
}
22+
if (code === 125) {
23+
effects.consume(code);
24+
effects.exit('literalJsDoclink');
25+
return ok(code);
26+
}
27+
28+
if (markdownLineEnding(code)) {
29+
return nok(code);
30+
}
31+
32+
effects.consume(code);
33+
return atext;
34+
}
35+
36+
return function (code) {
37+
effects.enter('literalJsDoclink');
38+
effects.consume(code);
39+
return atext;
40+
};
41+
}
42+
43+
const text = {};
44+
text[123] = {
45+
tokenize: tokenizeJsDoclink,
46+
previous(code) {
47+
return code === null || code === 32 || markdownLineEnding(code);
48+
}
49+
};
50+
51+
const linkRegExp = /\{@link\s+(.+?)(?:[\s|](.*?))?\}/;
52+
const tutorialRegExp = /\{@tutorial\s+(.+?)(?:[\s|](.*?))?\}/;
253

354
/**
455
* A remark plugin that installs
@@ -10,6 +61,7 @@ const findAndReplace = require('mdast-util-find-and-replace');
1061
* @returns {Function}
1162
*/
1263
module.exports = function () {
64+
const data = this.data();
1365
function replace(type) {
1466
return (match, matchUrl, matchValue) => {
1567
return {
@@ -27,10 +79,37 @@ module.exports = function () {
2779
};
2880
}
2981

30-
return function transform(markdownAST) {
31-
return findAndReplace(markdownAST, [
32-
[/\{@link\s+(.+?)(?:[\s|](.*?))?\}/g, replace('link')],
33-
[/\{@tutorial\s+(.+?)(?:[\s|](.*?))?\}/g, replace('tutorial')]
34-
]);
35-
};
82+
add('micromarkExtensions', { text });
83+
add('fromMarkdownExtensions', {
84+
transforms: [
85+
function (markdownAST) {
86+
return findAndReplace(markdownAST, [
87+
[new RegExp(linkRegExp.source, 'g'), replace('link')],
88+
[new RegExp(tutorialRegExp.source, 'g'), replace('tutorial')]
89+
]);
90+
}
91+
],
92+
enter: {
93+
literalJsDoclink(token) {
94+
const str = this.sliceSerialize(token);
95+
let match = null;
96+
if (str.startsWith('{@link')) {
97+
match = linkRegExp.exec(str);
98+
} else {
99+
match = tutorialRegExp.exec(str);
100+
}
101+
102+
this.enter(replace('link')(...match), token);
103+
}
104+
},
105+
exit: {
106+
literalJsDoclink(token) {
107+
this.exit(token);
108+
}
109+
}
110+
});
111+
function add(field, value) {
112+
if (data[field]) data[field].push(value);
113+
else data[field] = [value];
114+
}
36115
};

Diff for: src/remark-parse.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const remark = require('remark');
2-
const removePosition = require('./remark-remove-position')();
3-
const jsDocLink = require('./remark-jsDoc-link')();
2+
const gfm = require('remark-gfm');
3+
const removePosition = require('./remark-remove-position');
4+
const jsDocLink = require('./remark-jsDoc-link');
45

56
/**
67
* Parse a string of Markdown into a Remark
@@ -10,9 +11,4 @@ const jsDocLink = require('./remark-jsDoc-link')();
1011
* @returns {Object} abstract syntax tree
1112
* @private
1213
*/
13-
module.exports = function (string) {
14-
const treeAst = remark().parse(string);
15-
removePosition(treeAst);
16-
jsDocLink(treeAst);
17-
return treeAst;
18-
};
14+
module.exports = remark().use([jsDocLink, gfm, removePosition]).parse;

Diff for: src/remark-remove-position.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
const visit = require('unist-util-visit');
22

33
module.exports = function () {
4-
return function transform(markdownAST) {
5-
visit(markdownAST, node => delete node.position);
6-
return markdownAST;
7-
};
4+
const data = this.data();
5+
add('fromMarkdownExtensions', {
6+
transforms: [
7+
function (markdownAST) {
8+
visit(markdownAST, node => delete node.position);
9+
}
10+
]
11+
});
12+
function add(field, value) {
13+
if (data[field]) data[field].push(value);
14+
else data[field] = [value];
15+
}
816
};

0 commit comments

Comments
 (0)