fix(v1): markdown content and toc should render the same (#2022)

* fix(v1): autogenerated_toc and sidebar toc should render the same

* revert test docs

* yarn.lock
This commit is contained in:
Endi 2019-11-21 12:04:07 +07:00 committed by GitHub
parent 91902ed709
commit bd68dce667
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 23 deletions

View file

@ -39,22 +39,35 @@ describe('getTOC', () => {
expect(headingsJson).toContain('4th level headings'); expect(headingsJson).toContain('4th level headings');
}); });
describe('stripping of HTML', () => { test('html tag in source', () => {
test('correctly removes', () => { const headings = getTOC(`## <a name="foo"></a> Foo`, 'h2', []);
const headings = getTOC(`## <a name="foo"></a> Foo`, 'h2', []);
expect(headings[0].hashLink).toEqual('a-namefooa-foo'); expect(headings[0].hashLink).toEqual('a-namefooa-foo');
expect(headings[0].rawContent).toEqual(`<a name="foo"></a> Foo`); expect(headings[0].rawContent).toEqual(`<a name="foo"></a> Foo`);
expect(headings[0].content).toEqual('Foo'); expect(headings[0].content).toEqual(`<a name="foo"></a> Foo`);
}); });
test('retains formatting from Markdown', () => { test('transform markdown syntax to html syntax', () => {
const headings = getTOC(`## <a name="foo"></a> _Foo_`, 'h2', []); const headings = getTOC(`## <a name="foo"></a> _Foo_`, 'h2', []);
expect(headings[0].hashLink).toEqual('a-namefooa-_foo_'); expect(headings[0].hashLink).toEqual('a-namefooa-_foo_');
expect(headings[0].rawContent).toEqual(`<a name="foo"></a> _Foo_`); expect(headings[0].rawContent).toEqual(`<a name="foo"></a> _Foo_`);
expect(headings[0].content).toEqual('<em>Foo</em>'); expect(headings[0].content).toEqual(`<a name="foo"></a> <em>Foo</em>`);
});
const headings2 = getTOC(`## **Foo**`, 'h2', []);
expect(headings2[0].hashLink).toEqual('foo');
expect(headings2[0].rawContent).toEqual(`**Foo**`);
expect(headings2[0].content).toEqual(`<strong>Foo</strong>`);
});
test('does not strip tags randomly', () => {
// eslint-disable-next-line no-useless-escape
const headings = getTOC(`## function1 [array\<string>]`, 'h2', []);
expect(headings[0].hashLink).toEqual('function1-arraystring');
expect(headings[0].rawContent).toEqual(`function1 [array<string>]`);
expect(headings[0].content).toEqual(`function1 [array<string>]`);
}); });
}); });

View file

@ -7,7 +7,6 @@
const {Remarkable} = require('remarkable'); const {Remarkable} = require('remarkable');
const mdToc = require('markdown-toc'); const mdToc = require('markdown-toc');
const striptags = require('striptags');
const GithubSlugger = require('github-slugger'); const GithubSlugger = require('github-slugger');
const toSlug = require('./toSlug'); const toSlug = require('./toSlug');
@ -27,7 +26,10 @@ function getTOC(content, headingTags = 'h2', subHeadingTags = 'h3') {
? [].concat(subHeadingTags).map(tagToLevel) ? [].concat(subHeadingTags).map(tagToLevel)
: []; : [];
const allowedHeadingLevels = headingLevels.concat(subHeadingLevels); const allowedHeadingLevels = headingLevels.concat(subHeadingLevels);
const md = new Remarkable(); const md = new Remarkable({
// Enable HTML tags in source (same as './renderMarkdown.js')
html: true,
});
const headings = mdToc(content).json; const headings = mdToc(content).json;
const toc = []; const toc = [];
const slugger = new GithubSlugger(); const slugger = new GithubSlugger();
@ -35,8 +37,7 @@ function getTOC(content, headingTags = 'h2', subHeadingTags = 'h3') {
headings.forEach(heading => { headings.forEach(heading => {
const rawContent = heading.content; const rawContent = heading.content;
const safeContent = striptags(rawContent); const rendered = md.renderInline(rawContent);
const rendered = md.renderInline(safeContent);
const hashLink = toSlug(rawContent, slugger); const hashLink = toSlug(rawContent, slugger);
if (!allowedHeadingLevels.includes(heading.lvl)) { if (!allowedHeadingLevels.includes(heading.lvl)) {

View file

@ -70,7 +70,6 @@
"request": "^2.88.0", "request": "^2.88.0",
"shelljs": "^0.8.3", "shelljs": "^0.8.3",
"sitemap": "^3.2.2", "sitemap": "^3.2.2",
"striptags": "^3.1.1",
"tcp-port-used": "^1.0.1", "tcp-port-used": "^1.0.1",
"tiny-lr": "^1.1.1", "tiny-lr": "^1.1.1",
"tree-node-cli": "^1.2.5", "tree-node-cli": "^1.2.5",

View file

@ -15225,11 +15225,6 @@ strip-outer@^1.0.0:
dependencies: dependencies:
escape-string-regexp "^1.0.2" escape-string-regexp "^1.0.2"
striptags@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/striptags/-/striptags-3.1.1.tgz#c8c3e7fdd6fb4bb3a32a3b752e5b5e3e38093ebd"
integrity sha1-yMPn/db7S7OjKjt1LltePjgJPr0=
strong-log-transformer@^2.0.0: strong-log-transformer@^2.0.0:
version "2.1.0" version "2.1.0"
resolved "https://registry.yarnpkg.com/strong-log-transformer/-/strong-log-transformer-2.1.0.tgz#0f5ed78d325e0421ac6f90f7f10e691d6ae3ae10" resolved "https://registry.yarnpkg.com/strong-log-transformer/-/strong-log-transformer-2.1.0.tgz#0f5ed78d325e0421ac6f90f7f10e691d6ae3ae10"