Skip to content

Commit da05420

Browse files
committed
Complete correctHeadings, now trying to figure out why saveBook cause display problem.
1 parent 0fd059f commit da05420

5 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/app/book/markdown-elements/summary.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export class SummaryComponent implements OnInit {
4949

5050
return Promise.all(promises).then(files => {
5151
const _html = (files.reduce((acc: string, f: VFile): string => {
52-
return f && !f.notFound ? acc.concat(f.contents as string) : acc;
52+
return f && !f.notFound
53+
? acc.concat(f.contents as string)
54+
: acc;
5355
}, '') as string);
5456

5557
this.html = this.sanitizer.bypassSecurityTrustHtml(_html);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ export class TOCPaginationComponent implements OnInit, OnChanges {
119119
link,
120120
};
121121

122-
return _ && !_.data.gbr.notFound ? [...acc, fileIndexItem] : acc;
122+
return _ && !_.data.gbr.notFound && _.contents
123+
? [...acc, fileIndexItem]
124+
: acc;
123125

124126
}, []) as Array<FileIndexItem>;
125127
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import toString from 'mdast-util-to-string';
2323
import strip from 'remark-strip-html';
2424
import sectionize from 'remark-sectionize';
2525

26-
import { getTitle } from '../../gbr-preset';
26+
import { getTitle, correctHeadings } from '../../gbr-preset';
2727
import { tocPlugin } from '../plugins/toc';
2828
import { removeNodesPlugin } from '../plugins/remove';
2929
import { sectionPlugin } from '../plugins/sections';
@@ -80,6 +80,7 @@ export class MarkdownService {
8080
private get tocProcessor() {
8181
return remark()
8282
.use(frontmatter)
83+
.use(correctHeadings)
8384
.use(getTitle)
8485
.use(slug)
8586
.use(removeNodesPlugin)
@@ -95,6 +96,7 @@ export class MarkdownService {
9596
@lazyInitialize
9697
private get tocPaginationProcessor() {
9798
return remark()
99+
.use(correctHeadings)
98100
.use(getTitle)
99101
}
100102

src/app/gbr-preset/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import reporter from 'vfile-reporter';
1010
import sectionize from 'remark-sectionize';
1111
import externalLinks from 'remark-external-links';
1212

13-
import { readMatter, getTitle } from './plugins/frontmatter';
13+
import { readMatter, getTitle, correctHeadings } from './plugins/frontmatter';
1414
import { infoString, infoStringToAttr } from './plugins/misc';
1515
import { prism } from './plugins/prism';
1616
import { mermaid } from './plugins/mermaid';
@@ -20,7 +20,7 @@ import { Root } from 'mdast';
2020
import { VFile } from 'vfile';
2121
import { Transformer } from 'unified';
2222

23-
export { prism, mermaid, reporter, getTitle };
23+
export { prism, mermaid, reporter, getTitle, correctHeadings };
2424

2525
export function moveIds(): Transformer {
2626
return (tree: Root, file: VFile) => {
@@ -41,6 +41,7 @@ export const plugins = [
4141
gfm,
4242
parseFrontmatter,
4343
readMatter,
44+
correctHeadings,
4445
getTitle,
4546
infoString,
4647
slug,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function getTitle(): Transformer {
2929
{
3030
const heading: Heading = {
3131
type: 'heading',
32-
depth: 1,
32+
depth: 2,
3333
children: [{type: 'text', value: '未知标题'}]
3434
};
3535

@@ -49,8 +49,8 @@ export function getTitle(): Transformer {
4949
export function correctHeadings(): Transformer {
5050
return (tree: Root, file: VFile) => {
5151
return visit(tree, 'paragraph', (node: Paragraph, index: number, parent: any) => {
52-
5352
const text: string = node.children[0].value as string;
53+
5454
let re = new RegExp(/^(\#){1,3}/);
5555
if(re.test(text)){
5656
const hashes = text.match(/(\#){1,3}/)[0];

0 commit comments

Comments
 (0)