Skip to content

Commit 400b6b1

Browse files
committed
Fixed no heading exception.
1 parent c17c6fb commit 400b6b1

6 files changed

Lines changed: 47 additions & 38 deletions

File tree

src/app/book/gbr-preset/plugins/frontmatter.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,18 @@ export function readMatter(): Transformer {
2323

2424
export function getTitle(): Transformer {
2525
return (tree: Root, file: VFile) => {
26+
if(tree.children.findIndex(_ => _.type === 'heading') < 0 && !(/^\_/.test(file.basename))){
27+
const heading: Heading = {
28+
type: 'heading',
29+
depth: 1,
30+
children: [{type: 'text', value: '未知标题'}]
31+
};
32+
33+
tree.children.unshift(heading);
34+
}
35+
2636
file.data = file.data || {};
27-
return visit(tree, 'heading', (node: Heading) => {
37+
return visit(tree, 'heading', (node: Heading, index: number, parent: any) => {
2838
if (node.depth === 1 && !file.data.title) {
2939
file.data.title = toString(node);
3040
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class TOCPaginationComponent implements OnInit, OnChanges {
115115

116116
const fileIndexItem = {
117117
path,
118-
title: _.data.title || '无标题',
118+
title: _.data.title || '未知标题',
119119
link,
120120
};
121121

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ export class MarkdownService {
7979
private get tocProcessor() {
8080
return remark()
8181
.use(frontmatter)
82-
.use(slug)
8382
.use(getTitle)
83+
.use(slug)
8484
.use(removeNodesPlugin)
8585
.use(tocPlugin)
8686
.use(links, { locationService: this.locationService })

src/app/book/plugins/toc.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export function tocPlugin(options?: TOCOptions) {
2222
return visit(tree, 'listItem', (node: any, index: number, parent: any) => {
2323
if (node.children.length > 1) {
2424
node.data = node.data || {};
25-
node.data.title = node.data.title || '无标题';
2625
node.data.hProperties = node.data.hProperties || {};
2726
node.data.hProperties.class = node.data.hProperties.class || [];
2827
node.data.hProperties.class.push('has-children');

src/app/book/services/location.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class LocationService {
8484

8585
vfile.data = {
8686
gbr: {
87-
url: _url
87+
url: _url,
8888
}
8989
};
9090

src/app/book/shared/vfile.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,73 @@ import type * as toc from 'mdast-util-toc';
66
import type * as unified from 'unified';
77

88
interface UnknownData {
9-
[key: string]: unknown;
9+
[key: string]: unknown;
1010
}
1111

1212
export interface TOCData {
13-
name: string;
14-
url: string;
15-
link: string | string[];
16-
fragment: string;
17-
content: string;
18-
depth: number;
13+
name: string;
14+
url: string;
15+
link: string | string[];
16+
fragment: string;
17+
content: string;
18+
depth: number;
1919
}
2020

2121
interface FrontmatterData extends UnknownData {
22-
title: string;
23-
description: string;
24-
keywords: string;
25-
author: string;
22+
title: string;
23+
description: string;
24+
keywords: string;
25+
author: string;
2626
}
2727

2828
interface GbrData extends UnknownData {
29-
url: string;
30-
notFound?: boolean;
31-
isPageContent?: boolean;
29+
url: string;
30+
notFound?: boolean;
31+
isPageContent?: boolean;
3232
}
3333

3434
export interface TOCOptions extends toc.TOCOptions {
35-
minDepth?: number;
35+
minDepth?: number;
3636
}
3737

3838
export interface SectionData {
39-
id: string;
40-
text: string;
41-
heading: string;
42-
source: string;
43-
depth: number;
44-
name: string;
39+
id: string;
40+
text: string;
41+
heading: string;
42+
source: string;
43+
depth: number;
44+
name: string;
4545
}
4646

4747
interface VFileData extends UnknownData {
48-
tocOptions?: TOCOptions;
49-
tocSearch?: TOCData[];
50-
matter?: FrontmatterData;
51-
title?: string;
52-
gbr?: GbrData;
53-
sections?: SectionData[];
48+
tocOptions?: TOCOptions;
49+
tocSearch?: TOCData[];
50+
matter?: FrontmatterData;
51+
title?: string;
52+
gbr?: GbrData;
53+
sections?: SectionData[];
5454
}
5555

5656
export interface VFile extends vfile.VFile {
57-
data: VFileData;
57+
data: VFileData;
5858
}
5959

6060
export function VFile(doc: vfile.VFileCompatible): VFile {
61-
return VFILE(doc) as VFile;
61+
return VFILE(doc) as VFile;
6262
}
6363

6464
export function isVfile(x: unknown): x is VFile {
65-
return x instanceof VFILE;
65+
return x instanceof VFILE;
6666
}
6767

6868
export function getBasePath(_: VFile) {
69-
return _.history[0];
69+
return _.history[0];
7070
}
7171

7272
export function getFullPath(_: VFile) {
73-
return join(_.cwd, _.path);
73+
return join(_.cwd, _.path);
7474
}
7575

7676
export interface Preset extends unified.Preset {
77-
reporter?: (vfile: VFile) => {};
77+
reporter?: (vfile: VFile) => {};
7878
}

0 commit comments

Comments
 (0)