Skip to content

Commit d6ef57b

Browse files
authored
update: refine docisfy-ignore. (#2003)
1 parent 581f4a5 commit d6ef57b

File tree

4 files changed

+96
-49
lines changed

4 files changed

+96
-49
lines changed

src/core/render/compiler.js

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { tree as treeTpl } from './tpl';
55
import { genTree } from './gen-tree';
66
import { slugify } from './slugify';
77
import { emojify } from './emojify';
8-
import { getAndRemoveConfig, removeAtag } from './utils';
8+
import {
9+
getAndRemoveConfig,
10+
removeAtag,
11+
getAndRemoveDocisfyIgnorConfig,
12+
} from './utils';
913
import { imageCompiler } from './compiler/image';
1014
import { highlightCodeCompiler } from './compiler/code';
1115
import { paragraphCompiler } from './compiler/paragraph';
@@ -207,32 +211,15 @@ export class Compiler {
207211
*/
208212
origin.heading = renderer.heading = function (text, level) {
209213
let { str, config } = getAndRemoveConfig(text);
210-
const nextToc = { level, title: removeAtag(str) };
214+
const nextToc = { level, title: str };
211215

212-
if (/<!-- {docsify-ignore} -->/g.test(str)) {
213-
str = str.replace('<!-- {docsify-ignore} -->', '');
214-
nextToc.title = removeAtag(str);
215-
nextToc.ignoreSubHeading = true;
216-
}
217-
218-
if (/{docsify-ignore}/g.test(str)) {
219-
str = str.replace('{docsify-ignore}', '');
220-
nextToc.title = removeAtag(str);
221-
nextToc.ignoreSubHeading = true;
222-
}
223-
224-
if (/<!-- {docsify-ignore-all} -->/g.test(str)) {
225-
str = str.replace('<!-- {docsify-ignore-all} -->', '');
226-
nextToc.title = removeAtag(str);
227-
nextToc.ignoreAllSubs = true;
228-
}
229-
230-
if (/{docsify-ignore-all}/g.test(str)) {
231-
str = str.replace('{docsify-ignore-all}', '');
232-
nextToc.title = removeAtag(str);
233-
nextToc.ignoreAllSubs = true;
234-
}
216+
const { content, ignoreAllSubs, ignoreSubHeading } =
217+
getAndRemoveDocisfyIgnorConfig(str);
218+
str = content.trim();
235219

220+
nextToc.title = removeAtag(str);
221+
nextToc.ignoreAllSubs = ignoreAllSubs;
222+
nextToc.ignoreSubHeading = ignoreSubHeading;
236223
const slug = slugify(config.id || str);
237224
const url = router.toURL(router.getCurrentPath(), { id: slug });
238225
nextToc.slug = url;

src/core/render/compiler/headline.js

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
1-
import { getAndRemoveConfig, removeAtag } from '../utils';
1+
import {
2+
getAndRemoveConfig,
3+
removeAtag,
4+
getAndRemoveDocisfyIgnorConfig,
5+
} from '../utils';
26
import { slugify } from './slugify';
37

48
export const headingCompiler = ({ renderer, router, _self }) =>
59
(renderer.code = (text, level) => {
610
let { str, config } = getAndRemoveConfig(text);
7-
const nextToc = { level, title: removeAtag(str) };
11+
const nextToc = { level, title: str };
812

9-
if (/<!-- {docsify-ignore} -->/g.test(str)) {
10-
str = str.replace('<!-- {docsify-ignore} -->', '');
11-
nextToc.title = removeAtag(str);
12-
nextToc.ignoreSubHeading = true;
13-
}
13+
const { content, ignoreAllSubs, ignoreSubHeading } =
14+
getAndRemoveDocisfyIgnorConfig(str);
15+
str = content.trim();
1416

15-
if (/{docsify-ignore}/g.test(str)) {
16-
str = str.replace('{docsify-ignore}', '');
17-
nextToc.title = removeAtag(str);
18-
nextToc.ignoreSubHeading = true;
19-
}
20-
21-
if (/<!-- {docsify-ignore-all} -->/g.test(str)) {
22-
str = str.replace('<!-- {docsify-ignore-all} -->', '');
23-
nextToc.title = removeAtag(str);
24-
nextToc.ignoreAllSubs = true;
25-
}
26-
27-
if (/{docsify-ignore-all}/g.test(str)) {
28-
str = str.replace('{docsify-ignore-all}', '');
29-
nextToc.title = removeAtag(str);
30-
nextToc.ignoreAllSubs = true;
31-
}
17+
nextToc.title = removeAtag(str);
18+
nextToc.ignoreAllSubs = ignoreAllSubs;
19+
nextToc.ignoreSubHeading = ignoreSubHeading;
3220

3321
const slug = slugify(config.id || str);
3422
const url = router.toURL(router.getCurrentPath(), { id: slug });

src/core/render/utils.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,34 @@ export function getAndRemoveConfig(str = '') {
4848
export function removeAtag(str = '') {
4949
return str.replace(/(<\/?a.*?>)/gi, '');
5050
}
51+
52+
/**
53+
* Remove the docsifyIgnore configs and return the str
54+
* @param {string} str The string to deal with.
55+
*
56+
* @return {string} str The string after delete the docsifyIgnore configs.
57+
*/
58+
export function getAndRemoveDocisfyIgnorConfig(content = '') {
59+
let ignoreAllSubs, ignoreSubHeading;
60+
if (/<!-- {docsify-ignore} -->/g.test(content)) {
61+
content = content.replace('<!-- {docsify-ignore} -->', '');
62+
ignoreSubHeading = true;
63+
}
64+
65+
if (/{docsify-ignore}/g.test(content)) {
66+
content = content.replace('{docsify-ignore}', '');
67+
ignoreSubHeading = true;
68+
}
69+
70+
if (/<!-- {docsify-ignore-all} -->/g.test(content)) {
71+
content = content.replace('<!-- {docsify-ignore-all} -->', '');
72+
ignoreAllSubs = true;
73+
}
74+
75+
if (/{docsify-ignore-all}/g.test(content)) {
76+
content = content.replace('{docsify-ignore-all}', '');
77+
ignoreAllSubs = true;
78+
}
79+
80+
return { content, ignoreAllSubs, ignoreSubHeading };
81+
}

test/unit/render-util.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const {
22
removeAtag,
33
getAndRemoveConfig,
4+
getAndRemoveDocisfyIgnorConfig,
45
} = require('../../src/core/render/utils');
56

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

24+
// getAndRemoveDocisfyIgnorConfig()
25+
// ---------------------------------------------------------------------------
26+
describe('getAndRemoveDocisfyIgnorConfig()', () => {
27+
test('getAndRemoveDocisfyIgnorConfig from <!-- {docsify-ignore} -->', () => {
28+
const { content, ignoreAllSubs, ignoreSubHeading } =
29+
getAndRemoveDocisfyIgnorConfig(
30+
'My Ignore Title<!-- {docsify-ignore} -->'
31+
);
32+
expect(content).toBe('My Ignore Title');
33+
expect(ignoreSubHeading).toBeTruthy();
34+
expect(ignoreAllSubs === undefined).toBeTruthy();
35+
});
36+
37+
test('getAndRemoveDocisfyIgnorConfig from <!-- {docsify-ignore-all} -->', () => {
38+
const { content, ignoreAllSubs, ignoreSubHeading } =
39+
getAndRemoveDocisfyIgnorConfig(
40+
'My Ignore Title<!-- {docsify-ignore-all} -->'
41+
);
42+
expect(content).toBe('My Ignore Title');
43+
expect(ignoreAllSubs).toBeTruthy();
44+
expect(ignoreSubHeading === undefined).toBeTruthy();
45+
});
46+
47+
test('getAndRemoveDocisfyIgnorConfig from {docsify-ignore}', () => {
48+
const { content, ignoreAllSubs, ignoreSubHeading } =
49+
getAndRemoveDocisfyIgnorConfig('My Ignore Title{docsify-ignore}');
50+
expect(content).toBe('My Ignore Title');
51+
expect(ignoreSubHeading).toBeTruthy();
52+
expect(ignoreAllSubs === undefined).toBeTruthy();
53+
});
54+
55+
test('getAndRemoveDocisfyIgnorConfig from {docsify-ignore-all}', () => {
56+
const { content, ignoreAllSubs, ignoreSubHeading } =
57+
getAndRemoveDocisfyIgnorConfig('My Ignore Title{docsify-ignore-all}');
58+
expect(content).toBe('My Ignore Title');
59+
expect(ignoreAllSubs).toBeTruthy();
60+
expect(ignoreSubHeading === undefined).toBeTruthy();
61+
});
62+
});
63+
2364
// getAndRemoveConfig()
2465
// ---------------------------------------------------------------------------
2566
describe('getAndRemoveConfig()', () => {

0 commit comments

Comments
 (0)