fix(v2): toc should not be broken for heading with html inline code (#2067)

This commit is contained in:
Endi 2019-11-29 12:33:51 +07:00 committed by Yangshun Tay
parent 3b060ad171
commit d6143868a4
6 changed files with 60 additions and 2 deletions

View file

@ -12,6 +12,7 @@
"@babel/traverse": "^7.7.4", "@babel/traverse": "^7.7.4",
"@mdx-js/mdx": "^1.5.1", "@mdx-js/mdx": "^1.5.1",
"@mdx-js/react": "^1.5.1", "@mdx-js/react": "^1.5.1",
"escape-html": "^1.0.3",
"github-slugger": "^1.2.1", "github-slugger": "^1.2.1",
"gray-matter": "^4.0.2", "gray-matter": "^4.0.2",
"loader-utils": "^1.2.3", "loader-utils": "^1.2.3",

View file

@ -1,5 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`inline code should be escaped 1`] = `
"export const rightToc = [
{
value: '<code>&lt;Head /&gt;</code>',
id: 'head-',
children: [
{
value: '<code>&lt;Head&gt;Test&lt;/Head&gt;</code>',
id: 'headtesthead',
children: []
}
]
},
{
value: '<code>&lt;div /&gt;</code>',
id: 'div-',
children: []
},
{
value: '<code>&lt;div&gt; Test &lt;/div&gt;</code>',
id: 'div-test-div',
children: []
},
{
value: '<code>&lt;div&gt;&lt;i&gt;Test&lt;/i&gt;&lt;/div&gt;</code>',
id: 'divitestidiv',
children: []
}
];
## \`<Head />\`
### \`<Head>Test</Head>\`
## \`<div />\`
## \`<div> Test </div>\`
## \`<div><i>Test</i></div>\`
"
`;
exports[`non text phrasing content 1`] = ` exports[`non text phrasing content 1`] = `
"export const rightToc = [ "export const rightToc = [
{ {

View file

@ -0,0 +1,9 @@
## `<Head />`
### `<Head>Test</Head>`
## `<div />`
## `<div> Test </div>`
## `<div><i>Test</i></div>`

View file

@ -27,6 +27,11 @@ test('non text phrasing content', async () => {
expect(result).toMatchSnapshot(); expect(result).toMatchSnapshot();
}); });
test('inline code should be escaped', async () => {
const result = await processFixture('inline-code');
expect(result).toMatchSnapshot();
});
test('text content', async () => { test('text content', async () => {
const result = await processFixture('just-content'); const result = await processFixture('just-content');
expect(result).toMatchInlineSnapshot(` expect(result).toMatchInlineSnapshot(`

View file

@ -7,6 +7,7 @@
const toString = require('mdast-util-to-string'); const toString = require('mdast-util-to-string');
const visit = require('unist-util-visit'); const visit = require('unist-util-visit');
const escapeHtml = require('escape-html');
const slugs = require('github-slugger')(); const slugs = require('github-slugger')();
// https://github.com/syntax-tree/mdast#heading // https://github.com/syntax-tree/mdast#heading
@ -18,7 +19,7 @@ function toValue(node) {
case 'heading': case 'heading':
return node.children.map(toValue).join(''); return node.children.map(toValue).join('');
case 'inlineCode': case 'inlineCode':
return `<code>${node.value}</code>`; return `<code>${escapeHtml(node.value)}</code>`;
case 'emphasis': case 'emphasis':
return `<em>${node.children.map(toValue).join('')}</em>`; return `<em>${node.children.map(toValue).join('')}</em>`;
case 'strong': case 'strong':

View file

@ -5939,7 +5939,7 @@ es6-promisify@^5.0.0:
dependencies: dependencies:
es6-promise "^4.0.3" es6-promise "^4.0.3"
escape-html@~1.0.3: escape-html@^1.0.3, escape-html@~1.0.3:
version "1.0.3" version "1.0.3"
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988" resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"
integrity sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg= integrity sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=