update: refine docisfy-ignore. (#2003)

This commit is contained in:
Koy ['kɔɪ] 2023-03-29 14:05:51 +08:00 committed by GitHub
parent 581f4a56c4
commit d6ef57b85b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 49 deletions

View File

@ -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 (/<!-- {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;

View File

@ -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 });

View File

@ -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 };
}

View File

@ -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 <!-- {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()', () => {