Skip to content

Commit 90d283d

Browse files
fix: convert {docsify-ignore} and {docsify-ignore-all} to HTML comments (#1318)
* breaking: convert {docsify-ignore} and {docsify-ignore-all} to HTML comments Close #441 * chore: add ignore and ignore-all unit tests Co-authored-by: 沈唁 <[email protected]>
1 parent 952f4c9 commit 90d283d

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

docs/more-pages.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,24 @@ A custom sidebar can also automatically generate a table of contents by setting
114114

115115
## Ignoring Subheaders
116116

117-
When `subMaxLevel` is set, each header is automatically added to the table of contents by default. If you want to ignore a specific header, add `{docsify-ignore}` to it.
117+
When `subMaxLevel` is set, each header is automatically added to the table of contents by default. If you want to ignore a specific header, add `<!-- {docsify-ignore} -->` to it.
118118

119119
```markdown
120120
# Getting Started
121121

122-
## Header {docsify-ignore}
122+
## Header <!-- {docsify-ignore} -->
123123

124124
This header won't appear in the sidebar table of contents.
125125
```
126126

127-
To ignore all headers on a specific page, you can use `{docsify-ignore-all}` on the first header of the page.
127+
To ignore all headers on a specific page, you can use `<!-- {docsify-ignore-all} -->` on the first header of the page.
128128

129129
```markdown
130-
# Getting Started {docsify-ignore-all}
130+
# Getting Started <!-- {docsify-ignore-all} -->
131131

132132
## Header
133133

134134
This header won't appear in the sidebar table of contents.
135135
```
136136

137-
Both `{docsify-ignore}` and `{docsify-ignore-all}` will not be rendered on the page when used.
137+
Both `<!-- {docsify-ignore} -->` and `<!-- {docsify-ignore-all} -->` will not be rendered on the page when used.

src/core/render/compiler.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ export class Compiler {
208208
let { str, config } = getAndRemoveConfig(text);
209209
const nextToc = { level, title: str };
210210

211-
if (/{docsify-ignore}/g.test(str)) {
212-
str = str.replace('{docsify-ignore}', '');
211+
if (/<!-- {docsify-ignore} -->/g.test(str)) {
212+
str = str.replace('<!-- {docsify-ignore} -->', '');
213213
nextToc.title = str;
214214
nextToc.ignoreSubHeading = true;
215215
}
216216

217-
if (/{docsify-ignore-all}/g.test(str)) {
218-
str = str.replace('{docsify-ignore-all}', '');
217+
if (/<!-- {docsify-ignore-all} -->/g.test(str)) {
218+
str = str.replace('<!-- {docsify-ignore-all} -->', '');
219219
nextToc.title = str;
220220
nextToc.ignoreAllSubs = true;
221221
}

src/core/render/compiler/headline.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ export const headingCompiler = ({ renderer, router, _self }) =>
66
let { str, config } = getAndRemoveConfig(text);
77
const nextToc = { level, title: str };
88

9-
if (/{docsify-ignore}/g.test(str)) {
10-
str = str.replace('{docsify-ignore}', '');
9+
if (/<!-- {docsify-ignore} -->/g.test(str)) {
10+
str = str.replace('<!-- {docsify-ignore} -->', '');
1111
nextToc.title = str;
1212
nextToc.ignoreSubHeading = true;
1313
}
1414

15-
if (/{docsify-ignore-all}/g.test(str)) {
16-
str = str.replace('{docsify-ignore-all}', '');
15+
if (/<!-- {docsify-ignore-all} -->/g.test(str)) {
16+
str = str.replace('<!-- {docsify-ignore-all} -->', '');
1717
nextToc.title = str;
1818
nextToc.ignoreAllSubs = true;
1919
}

test/unit/render.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,43 @@ describe('render', function() {
251251
</h6>`
252252
);
253253
});
254+
255+
it('ignore', async function() {
256+
const { docsify } = await init();
257+
const output = docsify.compiler.compile(
258+
'## h2 tag <!-- {docsify-ignore} -->'
259+
);
260+
expectSameDom(
261+
output,
262+
`
263+
<h2 id="h2-tag">
264+
<a href="#/?id=h2-tag" data-id="h2-tag" class="anchor">
265+
<span>h2 tag </span>
266+
</a>
267+
</h2>`
268+
);
269+
});
270+
271+
it('ignore-all', async function() {
272+
const { docsify } = await init();
273+
const output = docsify.compiler.compile(
274+
`# h1 tag <!-- {docsify-ignore-all} -->` + `\n## h2 tag`
275+
);
276+
expectSameDom(
277+
output,
278+
`
279+
<h1 id="h1-tag">
280+
<a href="#/?id=h1-tag" data-id="h1-tag" class="anchor">
281+
<span>h1 tag </span>
282+
</a>
283+
</h1>
284+
<h2 id="h2-tag">
285+
<a href="#/?id=h2-tag" data-id="h2-tag" class="anchor">
286+
<span>h2 tag</span>
287+
</a>
288+
</h2>`
289+
);
290+
});
254291
});
255292

256293
describe('link', function() {

0 commit comments

Comments
 (0)