From d2dc9a70e498331c228e45a69560a7784a2f06e6 Mon Sep 17 00:00:00 2001 From: endiliey Date: Thu, 21 Nov 2019 11:24:10 +0700 Subject: [PATCH 1/3] fix(v1): autogenerated_toc and sidebar toc should render the same --- docs/getting-started-installation.md | 4 ++ .../lib/core/__tests__/toc.test.js | 45 ++++++++++++------- packages/docusaurus-1.x/lib/core/toc.js | 9 ++-- packages/docusaurus-1.x/package.json | 1 - 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/docs/getting-started-installation.md b/docs/getting-started-installation.md index fef732b4e350..d38a5f0ec20a 100644 --- a/docs/getting-started-installation.md +++ b/docs/getting-started-installation.md @@ -4,6 +4,10 @@ title: Installation description: Docusaurus was designed from the ground up to be easily installed and used to get your website up and running quickly! --- +## function1 [array\] + +## _Foo_ + Docusaurus was designed from the ground up to be easily installed and used to get your website up and running quickly. ## Installing Docusaurus diff --git a/packages/docusaurus-1.x/lib/core/__tests__/toc.test.js b/packages/docusaurus-1.x/lib/core/__tests__/toc.test.js index 131239c09637..2198edf60abc 100644 --- a/packages/docusaurus-1.x/lib/core/__tests__/toc.test.js +++ b/packages/docusaurus-1.x/lib/core/__tests__/toc.test.js @@ -39,22 +39,35 @@ describe('getTOC', () => { expect(headingsJson).toContain('4th level headings'); }); - describe('stripping of HTML', () => { - test('correctly removes', () => { - const headings = getTOC(`## Foo`, 'h2', []); - - expect(headings[0].hashLink).toEqual('a-namefooa-foo'); - expect(headings[0].rawContent).toEqual(` Foo`); - expect(headings[0].content).toEqual('Foo'); - }); - - test('retains formatting from Markdown', () => { - const headings = getTOC(`## _Foo_`, 'h2', []); - - expect(headings[0].hashLink).toEqual('a-namefooa-_foo_'); - expect(headings[0].rawContent).toEqual(` _Foo_`); - expect(headings[0].content).toEqual('Foo'); - }); + test('html tag in source', () => { + const headings = getTOC(`## Foo`, 'h2', []); + + expect(headings[0].hashLink).toEqual('a-namefooa-foo'); + expect(headings[0].rawContent).toEqual(` Foo`); + expect(headings[0].content).toEqual(` Foo`); + }); + + test('transform markdown syntax to html syntax', () => { + const headings = getTOC(`## _Foo_`, 'h2', []); + + expect(headings[0].hashLink).toEqual('a-namefooa-_foo_'); + expect(headings[0].rawContent).toEqual(` _Foo_`); + expect(headings[0].content).toEqual(` Foo`); + + const headings2 = getTOC(`## **Foo**`, 'h2', []); + + expect(headings2[0].hashLink).toEqual('foo'); + expect(headings2[0].rawContent).toEqual(`**Foo**`); + expect(headings2[0].content).toEqual(`Foo`); + }); + + test('does not strip tags randomly', () => { + // eslint-disable-next-line no-useless-escape + const headings = getTOC(`## function1 [array\]`, 'h2', []); + + expect(headings[0].hashLink).toEqual('function1-arraystring'); + expect(headings[0].rawContent).toEqual(`function1 [array]`); + expect(headings[0].content).toEqual(`function1 [array]`); }); }); diff --git a/packages/docusaurus-1.x/lib/core/toc.js b/packages/docusaurus-1.x/lib/core/toc.js index a4b22ef969db..c96656bcd0bb 100644 --- a/packages/docusaurus-1.x/lib/core/toc.js +++ b/packages/docusaurus-1.x/lib/core/toc.js @@ -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'); @@ -27,7 +26,10 @@ 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(); @@ -35,8 +37,7 @@ function getTOC(content, headingTags = 'h2', subHeadingTags = 'h3') { 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)) { diff --git a/packages/docusaurus-1.x/package.json b/packages/docusaurus-1.x/package.json index dc891390c235..454a01682d2f 100644 --- a/packages/docusaurus-1.x/package.json +++ b/packages/docusaurus-1.x/package.json @@ -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", From 7988079a7f7b19704d1d8d819e687bfa79b63d54 Mon Sep 17 00:00:00 2001 From: endiliey Date: Thu, 21 Nov 2019 11:38:43 +0700 Subject: [PATCH 2/3] revert test docs --- docs/getting-started-installation.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/getting-started-installation.md b/docs/getting-started-installation.md index d38a5f0ec20a..fef732b4e350 100644 --- a/docs/getting-started-installation.md +++ b/docs/getting-started-installation.md @@ -4,10 +4,6 @@ title: Installation description: Docusaurus was designed from the ground up to be easily installed and used to get your website up and running quickly! --- -## function1 [array\] - -## _Foo_ - Docusaurus was designed from the ground up to be easily installed and used to get your website up and running quickly. ## Installing Docusaurus From 8ebd1119b0ce8fd8018dd1dd936cdef9f012d3a6 Mon Sep 17 00:00:00 2001 From: endiliey Date: Thu, 21 Nov 2019 11:45:10 +0700 Subject: [PATCH 3/3] yarn.lock --- yarn.lock | 5 ----- 1 file changed, 5 deletions(-) diff --git a/yarn.lock b/yarn.lock index d0aed842a04a..aed108938b99 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"