Skip to content

Commit c17c6fb

Browse files
committed
Make toc-pagination.component stable.
1 parent 5162ca5 commit c17c6fb

3 files changed

Lines changed: 27 additions & 29 deletions

File tree

src/app/book/markdown-elements/toc-pagination.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ export class TOCPaginationComponent implements OnInit, OnChanges {
105105

106106
return Promise.all(p).then(files => {
107107
this.files = files.reduce((acc: Array<FileIndexItem>, _: VFile): Array<FileIndexItem> => {
108-
console.log(_);
109108
const path = getBasePath(_);
110109
let link: string | string[] = this.locationService.prepareLink(path, this.routerService.root);
111110

@@ -116,7 +115,7 @@ export class TOCPaginationComponent implements OnInit, OnChanges {
116115

117116
const fileIndexItem = {
118117
path,
119-
title: _.data.title,
118+
title: _.data.title || '无标题',
120119
link,
121120
};
122121

src/app/book/markdown/markdown.service.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ export class MarkdownService {
264264
}),
265265
map((_: any) => {
266266
return _.data.tocSearch.reduce((acc: Array<string>, __: any): Array<string> => {
267-
return __
267+
return __
268268
&& (/\.md$/.test(__.url.split('#')[0]))
269269
&& (acc.findIndex(path => path === (
270270
/^\//.test(__.url.split('#')[0])
@@ -273,7 +273,7 @@ export class MarkdownService {
273273
)) < 0)
274274
? [...acc, decodeURI(/^\//.test(__.url.split('#')[0]) ? __.url.split('#')[0] : `/${__.url.split('#')[0]}`)]
275275
: acc;
276-
}, []) as Array<string>;
276+
}, []) as Array<string>;
277277
}),
278278
catchError((err: any) => Observable.throw(err.json))
279279
).toPromise();
@@ -284,7 +284,6 @@ export class MarkdownService {
284284
const fullPath = await this.fetchService.find(_vfile.cwd, _vfile.path);
285285

286286
const pathsFromBackend: Array<string> = await this.loadSummaryFromBackend();
287-
288287
if(fullPath){
289288
const pathsFromFile: Array<string> = await this.loadSummaryFromFile(_vfile, fullPath);
290289

@@ -295,7 +294,6 @@ export class MarkdownService {
295294

296295
return pathsFromFile;
297296
}
298-
299297
return pathsFromBackend;
300298
}
301299

@@ -309,12 +307,12 @@ export class MarkdownService {
309307
vfile.contents = resource.contents;
310308
vfile.data = vfile.data || {};
311309
vfile.notFound = resource.notFound;
312-
/* const err = */ await this.processTOC(vfile, {
313-
minDepth: +minDepth,
310+
await this.processTOC(vfile, {
311+
minDepth: +minDepth,
314312
// @ts-ignore
315313
maxDepth: +maxDepth,
316314
tight: tight
317-
});
315+
});
318316
return vfile;
319317
}),
320318
);

src/app/book/plugins/toc.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@ import type * as mdast from 'mdast';
66
import type { TOCOptions, VFile } from '../shared/vfile';
77

88
export function tocPlugin(options?: TOCOptions) {
9-
return (tree: mdast.Root, vfile: VFile) => {
10-
const _options = {
11-
...options,
12-
...(vfile.data?.tocOptions || {})
13-
};
9+
return (tree: mdast.Root, vfile: VFile) => {
10+
const _options = {
11+
...options,
12+
...(vfile.data?.tocOptions || {})
13+
};
1414

15-
const result = toc(tree, _options);
15+
const result = toc(tree, _options);
1616

17-
tree.children = [].concat(
18-
tree.children.slice(0, result.index),
19-
result.map || []
20-
);
17+
tree.children = [].concat(
18+
tree.children.slice(0, result.index),
19+
result.map || []
20+
);
2121

22-
return visit(tree, 'listItem', (node: any, index: number, parent: any) => {
23-
if (node.children.length > 1) {
24-
node.data = node.data || {};
25-
node.data.hProperties = node.data.hProperties || {};
26-
node.data.hProperties.class = node.data.hProperties.class || [];
27-
node.data.hProperties.class.push('has-children');
28-
}
29-
return true;
30-
});
31-
};
22+
return visit(tree, 'listItem', (node: any, index: number, parent: any) => {
23+
if (node.children.length > 1) {
24+
node.data = node.data || {};
25+
node.data.title = node.data.title || '无标题';
26+
node.data.hProperties = node.data.hProperties || {};
27+
node.data.hProperties.class = node.data.hProperties.class || [];
28+
node.data.hProperties.class.push('has-children');
29+
}
30+
return true;
31+
});
32+
};
3233
}

0 commit comments

Comments
 (0)