diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index 255c49642..38e33b9f0 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -5,7 +5,11 @@ import { tree as treeTpl } from './tpl'; import { genTree } from './gen-tree'; import { slugify } from './slugify'; import { emojify } from './emojify'; -import { getAndRemoveConfig, removeAtag } from './utils'; +import { + getAndRemoveConfig, + removeAtag, + getAndRemoveDocisfyIgnorConfig, +} from './utils'; import { imageCompiler } from './compiler/image'; import { highlightCodeCompiler } from './compiler/code'; import { paragraphCompiler } from './compiler/paragraph'; @@ -207,32 +211,15 @@ export class Compiler { */ origin.heading = renderer.heading = function (text, level) { let { str, config } = getAndRemoveConfig(text); - const nextToc = { level, title: removeAtag(str) }; + const nextToc = { level, title: str }; - if (//g.test(str)) { - str = str.replace('', ''); - nextToc.title = removeAtag(str); - nextToc.ignoreSubHeading = true; - } - - if (/{docsify-ignore}/g.test(str)) { - str = str.replace('{docsify-ignore}', ''); - nextToc.title = removeAtag(str); - nextToc.ignoreSubHeading = true; - } - - if (//g.test(str)) { - str = str.replace('', ''); - nextToc.title = removeAtag(str); - nextToc.ignoreAllSubs = true; - } - - if (/{docsify-ignore-all}/g.test(str)) { - str = str.replace('{docsify-ignore-all}', ''); - nextToc.title = removeAtag(str); - nextToc.ignoreAllSubs = true; - } + const { content, ignoreAllSubs, ignoreSubHeading } = + getAndRemoveDocisfyIgnorConfig(str); + str = content.trim(); + nextToc.title = removeAtag(str); + nextToc.ignoreAllSubs = ignoreAllSubs; + nextToc.ignoreSubHeading = ignoreSubHeading; const slug = slugify(config.id || str); const url = router.toURL(router.getCurrentPath(), { id: slug }); nextToc.slug = url; diff --git a/src/core/render/compiler/headline.js b/src/core/render/compiler/headline.js index 61e4b3fb9..dafc2c98f 100644 --- a/src/core/render/compiler/headline.js +++ b/src/core/render/compiler/headline.js @@ -1,34 +1,22 @@ -import { getAndRemoveConfig, removeAtag } from '../utils'; +import { + getAndRemoveConfig, + removeAtag, + getAndRemoveDocisfyIgnorConfig, +} from '../utils'; import { slugify } from './slugify'; export const headingCompiler = ({ renderer, router, _self }) => (renderer.code = (text, level) => { let { str, config } = getAndRemoveConfig(text); - const nextToc = { level, title: removeAtag(str) }; + const nextToc = { level, title: str }; - if (//g.test(str)) { - str = str.replace('', ''); - nextToc.title = removeAtag(str); - nextToc.ignoreSubHeading = true; - } + const { content, ignoreAllSubs, ignoreSubHeading } = + getAndRemoveDocisfyIgnorConfig(str); + str = content.trim(); - if (/{docsify-ignore}/g.test(str)) { - str = str.replace('{docsify-ignore}', ''); - nextToc.title = removeAtag(str); - nextToc.ignoreSubHeading = true; - } - - if (//g.test(str)) { - str = str.replace('', ''); - nextToc.title = removeAtag(str); - nextToc.ignoreAllSubs = true; - } - - if (/{docsify-ignore-all}/g.test(str)) { - str = str.replace('{docsify-ignore-all}', ''); - nextToc.title = removeAtag(str); - nextToc.ignoreAllSubs = true; - } + nextToc.title = removeAtag(str); + nextToc.ignoreAllSubs = ignoreAllSubs; + nextToc.ignoreSubHeading = ignoreSubHeading; const slug = slugify(config.id || str); const url = router.toURL(router.getCurrentPath(), { id: slug }); diff --git a/src/core/render/utils.js b/src/core/render/utils.js index 42fbfa078..acd3a45e8 100644 --- a/src/core/render/utils.js +++ b/src/core/render/utils.js @@ -48,3 +48,34 @@ export function getAndRemoveConfig(str = '') { export function removeAtag(str = '') { return str.replace(/(<\/?a.*?>)/gi, ''); } + +/** + * Remove the docsifyIgnore configs and return the str + * @param {string} str The string to deal with. + * + * @return {string} str The string after delete the docsifyIgnore configs. + */ +export function getAndRemoveDocisfyIgnorConfig(content = '') { + let ignoreAllSubs, ignoreSubHeading; + if (//g.test(content)) { + content = content.replace('', ''); + ignoreSubHeading = true; + } + + if (/{docsify-ignore}/g.test(content)) { + content = content.replace('{docsify-ignore}', ''); + ignoreSubHeading = true; + } + + if (//g.test(content)) { + content = content.replace('', ''); + ignoreAllSubs = true; + } + + if (/{docsify-ignore-all}/g.test(content)) { + content = content.replace('{docsify-ignore-all}', ''); + ignoreAllSubs = true; + } + + return { content, ignoreAllSubs, ignoreSubHeading }; +} diff --git a/test/unit/render-util.test.js b/test/unit/render-util.test.js index 574e0a8ef..ff3377932 100644 --- a/test/unit/render-util.test.js +++ b/test/unit/render-util.test.js @@ -1,6 +1,7 @@ const { removeAtag, getAndRemoveConfig, + getAndRemoveDocisfyIgnorConfig, } = require('../../src/core/render/utils'); const { tree } = require(`../../src/core/render/tpl`); @@ -20,6 +21,46 @@ describe('core/render/utils', () => { }); }); + // getAndRemoveDocisfyIgnorConfig() + // --------------------------------------------------------------------------- + describe('getAndRemoveDocisfyIgnorConfig()', () => { + test('getAndRemoveDocisfyIgnorConfig from ', () => { + const { content, ignoreAllSubs, ignoreSubHeading } = + getAndRemoveDocisfyIgnorConfig( + 'My Ignore Title' + ); + expect(content).toBe('My Ignore Title'); + expect(ignoreSubHeading).toBeTruthy(); + expect(ignoreAllSubs === undefined).toBeTruthy(); + }); + + test('getAndRemoveDocisfyIgnorConfig from ', () => { + const { content, ignoreAllSubs, ignoreSubHeading } = + getAndRemoveDocisfyIgnorConfig( + 'My Ignore Title' + ); + expect(content).toBe('My Ignore Title'); + expect(ignoreAllSubs).toBeTruthy(); + expect(ignoreSubHeading === undefined).toBeTruthy(); + }); + + test('getAndRemoveDocisfyIgnorConfig from {docsify-ignore}', () => { + const { content, ignoreAllSubs, ignoreSubHeading } = + getAndRemoveDocisfyIgnorConfig('My Ignore Title{docsify-ignore}'); + expect(content).toBe('My Ignore Title'); + expect(ignoreSubHeading).toBeTruthy(); + expect(ignoreAllSubs === undefined).toBeTruthy(); + }); + + test('getAndRemoveDocisfyIgnorConfig from {docsify-ignore-all}', () => { + const { content, ignoreAllSubs, ignoreSubHeading } = + getAndRemoveDocisfyIgnorConfig('My Ignore Title{docsify-ignore-all}'); + expect(content).toBe('My Ignore Title'); + expect(ignoreAllSubs).toBeTruthy(); + expect(ignoreSubHeading === undefined).toBeTruthy(); + }); + }); + // getAndRemoveConfig() // --------------------------------------------------------------------------- describe('getAndRemoveConfig()', () => {