Skip to content

Commit

Permalink
fix: add gfm format for remark fixed #1374 (#1375)
Browse files Browse the repository at this point in the history
Plugins for remark was reworks because @link should be catch by first then autolink
  • Loading branch information
anthony-redFox authored Apr 24, 2021
1 parent 00d434f commit 6b5bc25
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 33 deletions.
80 changes: 80 additions & 0 deletions __tests__/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,16 @@ exports[`html nested.input.js 1`] = `

</li>


<li><a
href='#tableobj'
class=\\"\\">
tableObj

</a>

</li>

</ul>
</div>
<div class='mt1 h6 quiet'>
Expand Down Expand Up @@ -2451,6 +2461,76 @@ like a <a href=\\"#klass\\">klass</a>. This needs a <a href=\\"https://developer
</div>

</div>








</section>




<section class='p2 mb2 clearfix bg-white minishadow'>


<div class='clearfix'>

<h3 class='fl m0' id='tableobj'>
tableObj
</h3>


</div>


<table>
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dat 1</td>
<td>Dat 2</td>
<td>Dat 3</td>
</tr>
<tr>
<td>Dat 4</td>
<td>Dat 5</td>
<td>Dat 6</td>
</tr>
</tbody>
</table>

<div class='pre p1 fill-light mt0'>tableObj</div>
























Expand Down
8 changes: 8 additions & 0 deletions __tests__/fixture/html/nested.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,11 @@ var customStreams = {};
customStreams.passthrough = function() {
this.custom = true;
};

/**
* | Col 1 | Col 2 | Col 3 |
* |-------|-------|-------|
* | Dat 1 | Dat 2 | Dat 3 |
* | Dat 4 | Dat 5 | Dat 6 |
*/
var tableObj = {};
7 changes: 2 additions & 5 deletions __tests__/lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const parse = require('../../src/parsers/javascript');
const remarkP = require('remark')().parse;
const removePosition = require('../../src/remark-remove-position')();
const removePosition = require('../../src/remark-remove-position');
const remarkParse = require('remark')().use(removePosition).parse;
const visit = require('unist-util-visit');
const remarkParse = function (str) {
return removePosition(remarkP(str));
};

function pick(obj, props) {
if (Array.isArray(props)) {
Expand Down
7 changes: 2 additions & 5 deletions __tests__/lib/parsers/javascript.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const remarkP = require('remark')().parse;
const removePosition = require('../../../src/remark-remove-position')();
const removePosition = require('../../../src/remark-remove-position');
const remarkParse = require('remark')().use(removePosition).parse;
const parse = require('../../../src/parsers/javascript');
const remarkParse = function (str) {
return removePosition(remarkP(str));
};

function toComments(source, filename, opts) {
source = typeof source === 'string' ? source : '(' + source.toString() + ')';
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"pify": "^5.0.0",
"read-pkg-up": "^4.0.0",
"remark": "^13.0.0",
"remark-gfm": "^1.0.0",
"remark-html": "^13.0.1",
"remark-reference-links": "^5.0.0",
"remark-toc": "^7.2.0",
Expand All @@ -55,8 +56,8 @@
"yargs": "^15.3.1"
},
"optionalDependencies": {
"vue-template-compiler": "^2.6.12",
"@vue/compiler-sfc": "^3.0.11"
"@vue/compiler-sfc": "^3.0.11",
"vue-template-compiler": "^2.6.12"
},
"devDependencies": {
"chdir": "0.0.0",
Expand Down
91 changes: 85 additions & 6 deletions src/remark-jsDoc-link.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
const findAndReplace = require('mdast-util-find-and-replace');
const markdownLineEnding = require('micromark/dist/character/markdown-line-ending');

const link = '@link';
const tutorial = '@tutorial';

function tokenizeJsDoclink(effects, ok, nok) {
let index = 0;
let focus = link;

function atext(code) {
if (index !== link.length) {
if (focus.charCodeAt(index) === code) {
effects.consume(code);
index++;
return atext;
} else if (tutorial.charCodeAt(index) === code) {
focus = tutorial;
}
return nok(code);
}
if (code === 125) {
effects.consume(code);
effects.exit('literalJsDoclink');
return ok(code);
}

if (markdownLineEnding(code)) {
return nok(code);
}

effects.consume(code);
return atext;
}

return function (code) {
effects.enter('literalJsDoclink');
effects.consume(code);
return atext;
};
}

const text = {};
text[123] = {
tokenize: tokenizeJsDoclink,
previous(code) {
return code === null || code === 32 || markdownLineEnding(code);
}
};

const linkRegExp = /\{@link\s+(.+?)(?:[\s|](.*?))?\}/;
const tutorialRegExp = /\{@tutorial\s+(.+?)(?:[\s|](.*?))?\}/;

/**
* A remark plugin that installs
Expand All @@ -10,6 +61,7 @@ const findAndReplace = require('mdast-util-find-and-replace');
* @returns {Function}
*/
module.exports = function () {
const data = this.data();
function replace(type) {
return (match, matchUrl, matchValue) => {
return {
Expand All @@ -27,10 +79,37 @@ module.exports = function () {
};
}

return function transform(markdownAST) {
return findAndReplace(markdownAST, [
[/\{@link\s+(.+?)(?:[\s|](.*?))?\}/g, replace('link')],
[/\{@tutorial\s+(.+?)(?:[\s|](.*?))?\}/g, replace('tutorial')]
]);
};
add('micromarkExtensions', { text });
add('fromMarkdownExtensions', {
transforms: [
function (markdownAST) {
return findAndReplace(markdownAST, [
[new RegExp(linkRegExp.source, 'g'), replace('link')],
[new RegExp(tutorialRegExp.source, 'g'), replace('tutorial')]
]);
}
],
enter: {
literalJsDoclink(token) {
const str = this.sliceSerialize(token);
let match = null;
if (str.startsWith('{@link')) {
match = linkRegExp.exec(str);
} else {
match = tutorialRegExp.exec(str);
}

this.enter(replace('link')(...match), token);
}
},
exit: {
literalJsDoclink(token) {
this.exit(token);
}
}
});
function add(field, value) {
if (data[field]) data[field].push(value);
else data[field] = [value];
}
};
12 changes: 4 additions & 8 deletions src/remark-parse.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const remark = require('remark');
const removePosition = require('./remark-remove-position')();
const jsDocLink = require('./remark-jsDoc-link')();
const gfm = require('remark-gfm');
const removePosition = require('./remark-remove-position');
const jsDocLink = require('./remark-jsDoc-link');

/**
* Parse a string of Markdown into a Remark
Expand All @@ -10,9 +11,4 @@ const jsDocLink = require('./remark-jsDoc-link')();
* @returns {Object} abstract syntax tree
* @private
*/
module.exports = function (string) {
const treeAst = remark().parse(string);
removePosition(treeAst);
jsDocLink(treeAst);
return treeAst;
};
module.exports = remark().use([jsDocLink, gfm, removePosition]).parse;
16 changes: 12 additions & 4 deletions src/remark-remove-position.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
const visit = require('unist-util-visit');

module.exports = function () {
return function transform(markdownAST) {
visit(markdownAST, node => delete node.position);
return markdownAST;
};
const data = this.data();
add('fromMarkdownExtensions', {
transforms: [
function (markdownAST) {
visit(markdownAST, node => delete node.position);
}
]
});
function add(field, value) {
if (data[field]) data[field].push(value);
else data[field] = [value];
}
};
Loading

0 comments on commit 6b5bc25

Please sign in to comment.