Skip to content

Commit b9f4f5c

Browse files
committed
fix: autoheader config heading generate func
1 parent 49f5c56 commit b9f4f5c

File tree

2 files changed

+110
-9
lines changed

2 files changed

+110
-9
lines changed

src/core/render/compiler.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,16 @@ export class Compiler {
7474
this.linkTarget === '_blank' ? config.externalLinkRel || 'noopener' : '';
7575
this.contentBase = router.getBasePath();
7676

77-
const renderer = this._initRenderer();
78-
this.heading = renderer.heading;
77+
this.renderer = this._initRenderer();
7978
let compile;
8079
const mdConf = config.markdown || {};
8180

8281
if (isFn(mdConf)) {
83-
compile = mdConf(marked, renderer);
82+
compile = mdConf(marked, this.renderer);
8483
} else {
8584
marked.setOptions(
8685
Object.assign(mdConf, {
87-
renderer: Object.assign(renderer, mdConf.renderer),
86+
renderer: Object.assign(this.renderer, mdConf.renderer),
8887
}),
8988
);
9089
compile = marked;
@@ -318,12 +317,21 @@ export class Compiler {
318317
return treeTpl(tree);
319318
}
320319

320+
/**
321+
* Compile the text to generate HTML heading element based on the level
322+
* @param {*} text Text content, for now it is only from the _sidebar.md file
323+
* @param {*} level Type of heading (h<level> tag), for now it is always 1
324+
* @returns
325+
*/
321326
header(text, level) {
322-
return this.heading(text, level);
323-
}
324-
325-
article(text) {
326-
return this.compile(text);
327+
const tokenHeading = {
328+
type: 'heading',
329+
raw: text,
330+
depth: level,
331+
text: text,
332+
tokens: [{ type: 'text', raw: text, text: text }],
333+
};
334+
return this.renderer.heading(tokenHeading);
327335
}
328336

329337
/**

test/e2e/sidebar.test.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,96 @@ test.describe('Sidebar Tests', () => {
6969
expect(page.url()).toMatch(/\/test%3Efoo$/);
7070
});
7171
});
72+
73+
test.describe('Configuration: autoHeader', () => {
74+
test('autoHeader=false', async ({ page }) => {
75+
const docsifyInitConfig = {
76+
config: {
77+
loadSidebar: '_sidebar.md',
78+
autoHeader: false,
79+
},
80+
markdown: {
81+
sidebar: `
82+
- [QuickStartAutoHeader](quickstart.md)
83+
`,
84+
},
85+
routes: {
86+
'/quickstart.md': `
87+
the content of quickstart space
88+
## In the main content there is no h1
89+
`,
90+
},
91+
};
92+
93+
await docsifyInit(docsifyInitConfig);
94+
95+
await page.click('a[href="#/quickstart"]');
96+
expect(page.url()).toMatch(/\/quickstart$/);
97+
// const element = page.locator('#main');
98+
// expect(await element.innerText()).toContain(
99+
// 'In the main content there is no h1',
100+
// );
101+
// expect(await element.innerText()).toContain(
102+
// 'the content of quickstart space',
103+
// );
104+
// not heading
105+
await expect(page.locator('#quickstart')).toBeHidden();
106+
});
107+
108+
test('autoHeader=true', async ({ page }) => {
109+
const docsifyInitConfig = {
110+
config: {
111+
loadSidebar: '_sidebar.md',
112+
autoHeader: true,
113+
},
114+
markdown: {
115+
sidebar: `
116+
- [QuickStartAutoHeader](quickstart.md )
117+
`,
118+
},
119+
routes: {
120+
'/quickstart.md': `
121+
the content of quickstart space
122+
## In the main content there is no h1
123+
`,
124+
},
125+
};
126+
127+
await docsifyInit(docsifyInitConfig);
128+
129+
await page.click('a[href="#/quickstart"]');
130+
expect(page.url()).toMatch(/\/quickstart$/);
131+
132+
// auto generate default heading id
133+
const autoHeader = page.locator('#quickstartautoheader');
134+
expect(await autoHeader.innerText()).toContain('QuickStartAutoHeader');
135+
});
136+
137+
test('autoHeader=true and custom headingId', async ({ page }) => {
138+
const docsifyInitConfig = {
139+
config: {
140+
loadSidebar: '_sidebar.md',
141+
autoHeader: true,
142+
},
143+
markdown: {
144+
sidebar: `
145+
- [QuickStartAutoHeader](quickstart.md ":id=quickstartId")
146+
`,
147+
},
148+
routes: {
149+
'/quickstart.md': `
150+
the content of quickstart space
151+
## In the main content there is no h1
152+
`,
153+
},
154+
};
155+
156+
await docsifyInit(docsifyInitConfig);
157+
158+
await page.click('a[href="#/quickstart"]');
159+
expect(page.url()).toMatch(/\/quickstart$/);
160+
// auto generate custom heading id
161+
const autoHeader = page.locator('#quickstartId');
162+
expect(await autoHeader.innerText()).toContain('QuickStartAutoHeader');
163+
});
164+
});

0 commit comments

Comments
 (0)