feat(parser): support custom page header text

This commit is contained in:
syy11cn 2022-06-03 14:56:02 +08:00
parent cd5ac19179
commit 927cf6fb64
1 changed files with 9 additions and 8 deletions

View File

@ -1,6 +1,6 @@
import type Token from 'markdown-it/lib/token' import type Token from 'markdown-it/lib/token'
import MarkdownIt from 'markdown-it' import MarkdownIt from 'markdown-it'
import type { ISectionOptions, IStylesOptions, Table } from 'docx' import type { ISectionOptions, Table } from 'docx'
import { AlignmentType, BorderStyle, Document, Footer, Header, LevelFormat, Packer, PageNumber, Paragraph, SectionType, StyleLevel, TableOfContents, TextRun, convertInchesToTwip } from 'docx' import { AlignmentType, BorderStyle, Document, Footer, Header, LevelFormat, Packer, PageNumber, Paragraph, SectionType, StyleLevel, TableOfContents, TextRun, convertInchesToTwip } from 'docx'
import type { IMarkdownReportConfig } from '@md-report/types' import type { IMarkdownReportConfig } from '@md-report/types'
import { StyleId } from '@md-report/types' import { StyleId } from '@md-report/types'
@ -17,16 +17,16 @@ export async function getBuffer(props: { markdown: string; config: IMarkdownRepo
export function parse(props: { markdown: string; config: IMarkdownReportConfig }): Document { export function parse(props: { markdown: string; config: IMarkdownReportConfig }): Document {
const { markdown, config } = props const { markdown, config } = props
const { styles } = config
// Get frontmatter. // Get frontmatter.
// Get tokens. // Get tokens.
const tokens: Token[] = md.parse(markdown, {}) const tokens: Token[] = md.parse(markdown, {})
return parseDocument(tokens, styles) return parseDocument(tokens, config)
} }
export function parseDocument(tokens: Token[], styles: IStylesOptions): Document { export function parseDocument(tokens: Token[], config: IMarkdownReportConfig): Document {
// Variables. // Variables.
let pos = 0 let pos = 0
const { styles, meta } = config
// With default content section. // With default content section.
const sections: ISectionOptions[] = [{ const sections: ISectionOptions[] = [{
properties: { properties: {
@ -66,7 +66,7 @@ export function parseDocument(tokens: Token[], styles: IStylesOptions): Document
}, },
}, },
// TODO: replace with document title. // TODO: replace with document title.
text: '企业实习中期报告', text: meta.pageHeaderText,
})], })],
}), }),
}, },
@ -107,7 +107,7 @@ export function parseDocument(tokens: Token[], styles: IStylesOptions): Document
// Split and parse sections. // Split and parse sections.
while (pos < tokens.length) { while (pos < tokens.length) {
const { tokens: section, offset } = sliceSection(tokens.slice(pos)) const { tokens: section, offset } = sliceSection(tokens.slice(pos))
sections.push(parseSection(section)) sections.push(parseSection(section, config))
pos += offset pos += offset
} }
return new Document({ return new Document({
@ -233,9 +233,10 @@ export function parseDocument(tokens: Token[], styles: IStylesOptions): Document
}) })
} }
export function parseSection(tokens: Token[]): ISectionOptions { export function parseSection(tokens: Token[], config: IMarkdownReportConfig): ISectionOptions {
// Variables. // Variables.
let pos = 0 let pos = 0
const { meta } = config
const children: (Paragraph | Table | TableOfContents)[] = [] const children: (Paragraph | Table | TableOfContents)[] = []
const { content: sectionHeader = '' } = tokens[1] const { content: sectionHeader = '' } = tokens[1]
// Split and parse paragraphs. // Split and parse paragraphs.
@ -286,7 +287,7 @@ export function parseSection(tokens: Token[]): ISectionOptions {
}, },
}, },
// TODO: replace with document title. // TODO: replace with document title.
text: '企业实习中期报告', text: meta.pageHeaderText,
})], })],
}), }),
}, },