Skip to content

Commit 09e85d4

Browse files
Bowl42sy-records
andauthored
fix: exclude headings inside blockquotes from sidebar TOC (#2691)
Co-authored-by: Luffy <lufei@docsifyjs.org>
1 parent 54930e6 commit 09e85d4

4 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎src/core/render/compiler.js‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class Compiler {
2525
this.router = router;
2626
this.cacheTree = {};
2727
this.toc = [];
28+
this.blockquoteDepth = 0;
2829
this.cacheTOC = {};
2930
this.linkTarget = config.externalLinkTarget || '_blank';
3031
this.linkRel =
@@ -113,9 +114,13 @@ export class Compiler {
113114
}
114115

115116
let media;
116-
if (config.type && (media = compileMedia[config.type])) {
117+
const mediaType = Array.isArray(config.type)
118+
? config.type[0]
119+
: config.type;
120+
121+
if (mediaType && (media = compileMedia[mediaType])) {
117122
embed = media.call(this, href, title);
118-
embed.type = config.type;
123+
embed.type = mediaType;
119124
} else {
120125
let type = 'code';
121126
if (/\.(md|markdown)/.test(href)) {
@@ -165,7 +170,10 @@ export class Compiler {
165170
router,
166171
compiler: this,
167172
});
168-
origin.blockquoteCompiler = blockquoteCompiler({ renderer });
173+
origin.blockquoteCompiler = blockquoteCompiler({
174+
renderer,
175+
compiler: this,
176+
});
169177
origin.code = highlightCodeCompiler({ renderer });
170178
origin.link = linkCompiler({
171179
renderer,

‎src/core/render/compiler/blockquote.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const blockquoteCompiler = ({ renderer }) =>
1+
export const blockquoteCompiler = ({ renderer, compiler }) =>
22
(renderer.blockquote = function ({ tokens }) {
33
let openTag = '<blockquote>';
44
let closeTag = '</blockquote>';
@@ -40,6 +40,14 @@ export const blockquoteCompiler = ({ renderer }) =>
4040
}
4141
}
4242

43-
const body = this.parser.parse(tokens);
43+
compiler.blockquoteDepth++;
44+
let body = '';
45+
46+
try {
47+
body = this.parser.parse(tokens);
48+
} finally {
49+
compiler.blockquoteDepth--;
50+
}
51+
4452
return `${openTag}${body}${closeTag}`;
4553
});

‎src/core/render/compiler/heading.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export const headingCompiler = ({ renderer, router, compiler }) =>
2222
const slug = slugify(config.id || text);
2323
const url = router.toURL(router.getCurrentPath(), { id: slug });
2424
nextToc.slug = stripUrlExceptId(url);
25-
compiler.toc.push(nextToc);
25+
26+
if (compiler.blockquoteDepth === 0) {
27+
compiler.toc.push(nextToc);
28+
}
2629

2730
// Note: tabindex="-1" allows programmatically focusing on heading
2831
// elements after navigation. This is preferred over focusing on the link

‎test/integration/sidebar.test.js‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ describe('Test sidebar render toc structure', function () {
6363
},
6464
markdown: {
6565
homepage: `
66-
# Getting started
66+
# Getting started
6767
some thing
68-
## Level1
68+
## Level1
6969
foo
70-
### Level2
70+
### Level2
7171
bar
7272
`,
7373
},
@@ -118,4 +118,28 @@ describe('Test sidebar render toc structure', function () {
118118
expect(level1_A_tag.textContent).toContain('Level1');
119119
expect(level2_A_tag.textContent).toContain('Level2');
120120
});
121+
122+
test('Render sidebar should ignore headings in blockquote by default', async () => {
123+
await docsifyInit({
124+
config: {
125+
loadSidebar: false,
126+
},
127+
markdown: {
128+
homepage: `
129+
# Normal Title
130+
> # Quoted Title
131+
> #Quoted Title Without Space
132+
## Section Title
133+
`,
134+
},
135+
waitForSelector: '.sidebar-nav > ul',
136+
});
137+
138+
const sidebarText = document.querySelector('.sidebar-nav').textContent;
139+
140+
expect(sidebarText).toContain('Normal Title');
141+
expect(sidebarText).toContain('Section Title');
142+
expect(sidebarText).not.toContain('Quoted Title');
143+
expect(sidebarText).not.toContain('Quoted Title Without Space');
144+
});
121145
});

0 commit comments

Comments
 (0)