From 0e841172ad323b1c4ba9cbf13c56a09d1cc9e3f3 Mon Sep 17 00:00:00 2001 From: Vikrantsingh Thakur Date: Wed, 20 Mar 2019 00:15:59 +0530 Subject: [PATCH 1/4] fix: check for lowercase TOC token --- v1/lib/core/toc.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/v1/lib/core/toc.js b/v1/lib/core/toc.js index 691d202b09fa..7b643eb42d25 100644 --- a/v1/lib/core/toc.js +++ b/v1/lib/core/toc.js @@ -58,7 +58,12 @@ function getTOC(content, headingTags = 'h2', subHeadingTags = 'h3') { // takes the content of a doc article and returns the content with a table of // contents inserted function insertTOC(rawContent) { - if (!rawContent || rawContent.indexOf(TABLE_OF_CONTENTS_TOKEN) === -1) { + if (!rawContent) { + return rawContent; + } + const LOWERCASE_TOC_TOKEN = TABLE_OF_CONTENTS_TOKEN.toLowerCase(); + if(rawContent.indexOf(TABLE_OF_CONTENTS_TOKEN) === -1 && + rawContent.indexOf(LOWERCASE_TOC_TOKEN) === -1){ return rawContent; } const filterRe = /^`[^`]*`/; @@ -67,7 +72,9 @@ function insertTOC(rawContent) { .filter(header => filterRe.test(header.rawContent)) .map(header => ` - [${header.rawContent}](#${header.hashLink})`) .join('\n'); - return rawContent.replace(TABLE_OF_CONTENTS_TOKEN, tableOfContents); + return rawContent + .replace(TABLE_OF_CONTENTS_TOKEN, tableOfContents) + .replace(LOWERCASE_TOC_TOKEN, tableOfContents); } module.exports = { From dda320833d08e5bde36c9158e65f2744dac40bd6 Mon Sep 17 00:00:00 2001 From: Vikrantsingh Thakur Date: Wed, 20 Mar 2019 00:50:57 +0530 Subject: [PATCH 2/4] run prettier --- v1/lib/core/toc.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/v1/lib/core/toc.js b/v1/lib/core/toc.js index 7b643eb42d25..bff04b9f8964 100644 --- a/v1/lib/core/toc.js +++ b/v1/lib/core/toc.js @@ -62,8 +62,10 @@ function insertTOC(rawContent) { return rawContent; } const LOWERCASE_TOC_TOKEN = TABLE_OF_CONTENTS_TOKEN.toLowerCase(); - if(rawContent.indexOf(TABLE_OF_CONTENTS_TOKEN) === -1 && - rawContent.indexOf(LOWERCASE_TOC_TOKEN) === -1){ + if ( + rawContent.indexOf(TABLE_OF_CONTENTS_TOKEN) === -1 && + rawContent.indexOf(LOWERCASE_TOC_TOKEN) === -1 + ) { return rawContent; } const filterRe = /^`[^`]*`/; From 6a56578d1c7591808df9dd388150a4d66e81b185 Mon Sep 17 00:00:00 2001 From: endiliey Date: Wed, 20 Mar 2019 18:15:17 +0800 Subject: [PATCH 3/4] fix: handle case insensitive --- v1/lib/core/toc.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/v1/lib/core/toc.js b/v1/lib/core/toc.js index bff04b9f8964..598d4f223866 100644 --- a/v1/lib/core/toc.js +++ b/v1/lib/core/toc.js @@ -9,7 +9,10 @@ const Remarkable = require('remarkable'); const mdToc = require('markdown-toc'); const toSlug = require('./toSlug'); -const TABLE_OF_CONTENTS_TOKEN = ''; +const tableOfContentsRegex = new RegExp( + '', + 'i', +); /** * Returns a table of content from the headings @@ -61,11 +64,7 @@ function insertTOC(rawContent) { if (!rawContent) { return rawContent; } - const LOWERCASE_TOC_TOKEN = TABLE_OF_CONTENTS_TOKEN.toLowerCase(); - if ( - rawContent.indexOf(TABLE_OF_CONTENTS_TOKEN) === -1 && - rawContent.indexOf(LOWERCASE_TOC_TOKEN) === -1 - ) { + if (!tableOfContentsRegex.test(rawContent)) { return rawContent; } const filterRe = /^`[^`]*`/; @@ -74,9 +73,7 @@ function insertTOC(rawContent) { .filter(header => filterRe.test(header.rawContent)) .map(header => ` - [${header.rawContent}](#${header.hashLink})`) .join('\n'); - return rawContent - .replace(TABLE_OF_CONTENTS_TOKEN, tableOfContents) - .replace(LOWERCASE_TOC_TOKEN, tableOfContents); + return rawContent.replace(tableOfContentsRegex, tableOfContents); } module.exports = { From 163622da1c645e8cb94efbb91b6705858d1795db Mon Sep 17 00:00:00 2001 From: endiliey Date: Wed, 20 Mar 2019 18:17:44 +0800 Subject: [PATCH 4/4] nits --- v1/lib/core/toc.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/v1/lib/core/toc.js b/v1/lib/core/toc.js index 598d4f223866..9ee9a704575a 100644 --- a/v1/lib/core/toc.js +++ b/v1/lib/core/toc.js @@ -9,10 +9,7 @@ const Remarkable = require('remarkable'); const mdToc = require('markdown-toc'); const toSlug = require('./toSlug'); -const tableOfContentsRegex = new RegExp( - '', - 'i', -); +const tocRegex = new RegExp('', 'i'); /** * Returns a table of content from the headings @@ -61,10 +58,7 @@ function getTOC(content, headingTags = 'h2', subHeadingTags = 'h3') { // takes the content of a doc article and returns the content with a table of // contents inserted function insertTOC(rawContent) { - if (!rawContent) { - return rawContent; - } - if (!tableOfContentsRegex.test(rawContent)) { + if (!rawContent || !tocRegex.test(rawContent)) { return rawContent; } const filterRe = /^`[^`]*`/; @@ -73,7 +67,7 @@ function insertTOC(rawContent) { .filter(header => filterRe.test(header.rawContent)) .map(header => ` - [${header.rawContent}](#${header.hashLink})`) .join('\n'); - return rawContent.replace(tableOfContentsRegex, tableOfContents); + return rawContent.replace(tocRegex, tableOfContents); } module.exports = {