Skip to content

Commit

Permalink
fix(v1): markdown content and toc should render the same (#2022)
Browse files Browse the repository at this point in the history
* fix(v1): autogenerated_toc and sidebar toc should render the same

* revert test docs

* yarn.lock
  • Loading branch information
endiliey authored Nov 21, 2019
1 parent 91902ed commit bd68dce
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
45 changes: 29 additions & 16 deletions packages/docusaurus-1.x/lib/core/__tests__/toc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,35 @@ describe('getTOC', () => {
expect(headingsJson).toContain('4th level headings');
});

describe('stripping of HTML', () => {
test('correctly removes', () => {
const headings = getTOC(`## <a name="foo"></a> Foo`, 'h2', []);

expect(headings[0].hashLink).toEqual('a-namefooa-foo');
expect(headings[0].rawContent).toEqual(`<a name="foo"></a> Foo`);
expect(headings[0].content).toEqual('Foo');
});

test('retains formatting from Markdown', () => {
const headings = getTOC(`## <a name="foo"></a> _Foo_`, 'h2', []);

expect(headings[0].hashLink).toEqual('a-namefooa-_foo_');
expect(headings[0].rawContent).toEqual(`<a name="foo"></a> _Foo_`);
expect(headings[0].content).toEqual('<em>Foo</em>');
});
test('html tag in source', () => {
const headings = getTOC(`## <a name="foo"></a> Foo`, 'h2', []);

expect(headings[0].hashLink).toEqual('a-namefooa-foo');
expect(headings[0].rawContent).toEqual(`<a name="foo"></a> Foo`);
expect(headings[0].content).toEqual(`<a name="foo"></a> Foo`);
});

test('transform markdown syntax to html syntax', () => {
const headings = getTOC(`## <a name="foo"></a> _Foo_`, 'h2', []);

expect(headings[0].hashLink).toEqual('a-namefooa-_foo_');
expect(headings[0].rawContent).toEqual(`<a name="foo"></a> _Foo_`);
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>]`);
});
});

Expand Down
9 changes: 5 additions & 4 deletions packages/docusaurus-1.x/lib/core/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

const {Remarkable} = require('remarkable');
const mdToc = require('markdown-toc');
const striptags = require('striptags');
const GithubSlugger = require('github-slugger');
const toSlug = require('./toSlug');

Expand All @@ -27,16 +26,18 @@ function getTOC(content, headingTags = 'h2', subHeadingTags = 'h3') {
? [].concat(subHeadingTags).map(tagToLevel)
: [];
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 toc = [];
const slugger = new GithubSlugger();
let current;

headings.forEach(heading => {
const rawContent = heading.content;
const safeContent = striptags(rawContent);
const rendered = md.renderInline(safeContent);
const rendered = md.renderInline(rawContent);

const hashLink = toSlug(rawContent, slugger);
if (!allowedHeadingLevels.includes(heading.lvl)) {
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-1.x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"request": "^2.88.0",
"shelljs": "^0.8.3",
"sitemap": "^3.2.2",
"striptags": "^3.1.1",
"tcp-port-used": "^1.0.1",
"tiny-lr": "^1.1.1",
"tree-node-cli": "^1.2.5",
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15225,11 +15225,6 @@ strip-outer@^1.0.0:
dependencies:
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:
version "2.1.0"
resolved "https://registry.yarnpkg.com/strong-log-transformer/-/strong-log-transformer-2.1.0.tgz#0f5ed78d325e0421ac6f90f7f10e691d6ae3ae10"
Expand Down

0 comments on commit bd68dce

Please sign in to comment.