Skip to content

Commit

Permalink
update: refine docisfy-ignore. (docsifyjs#2003)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koooooo-7 authored Mar 29, 2023
1 parent 581f4a5 commit d6ef57b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 49 deletions.
37 changes: 12 additions & 25 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 (/<!-- {docsify-ignore} -->/g.test(str)) {
str = str.replace('<!-- {docsify-ignore} -->', '');
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 (/<!-- {docsify-ignore-all} -->/g.test(str)) {
str = str.replace('<!-- {docsify-ignore-all} -->', '');
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;
Expand Down
36 changes: 12 additions & 24 deletions src/core/render/compiler/headline.js
Original file line number Diff line number Diff line change
@@ -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 (/<!-- {docsify-ignore} -->/g.test(str)) {
str = str.replace('<!-- {docsify-ignore} -->', '');
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 (/<!-- {docsify-ignore-all} -->/g.test(str)) {
str = str.replace('<!-- {docsify-ignore-all} -->', '');
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 });
Expand Down
31 changes: 31 additions & 0 deletions src/core/render/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (/<!-- {docsify-ignore} -->/g.test(content)) {
content = content.replace('<!-- {docsify-ignore} -->', '');
ignoreSubHeading = true;
}

if (/{docsify-ignore}/g.test(content)) {
content = content.replace('{docsify-ignore}', '');
ignoreSubHeading = true;
}

if (/<!-- {docsify-ignore-all} -->/g.test(content)) {
content = content.replace('<!-- {docsify-ignore-all} -->', '');
ignoreAllSubs = true;
}

if (/{docsify-ignore-all}/g.test(content)) {
content = content.replace('{docsify-ignore-all}', '');
ignoreAllSubs = true;
}

return { content, ignoreAllSubs, ignoreSubHeading };
}
41 changes: 41 additions & 0 deletions test/unit/render-util.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
removeAtag,
getAndRemoveConfig,
getAndRemoveDocisfyIgnorConfig,
} = require('../../src/core/render/utils');

const { tree } = require(`../../src/core/render/tpl`);
Expand All @@ -20,6 +21,46 @@ describe('core/render/utils', () => {
});
});

// getAndRemoveDocisfyIgnorConfig()
// ---------------------------------------------------------------------------
describe('getAndRemoveDocisfyIgnorConfig()', () => {
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();
});

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()', () => {
Expand Down

0 comments on commit d6ef57b

Please sign in to comment.