From 927cf6fb64057d751f3090f71585bba9851384d8 Mon Sep 17 00:00:00 2001 From: syy11cn Date: Fri, 3 Jun 2022 14:56:02 +0800 Subject: [PATCH] feat(parser): support custom page header text --- packages/parser/src/index.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/parser/src/index.ts b/packages/parser/src/index.ts index f465920..f48c78c 100644 --- a/packages/parser/src/index.ts +++ b/packages/parser/src/index.ts @@ -1,6 +1,6 @@ import type Token from 'markdown-it/lib/token' 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 type { IMarkdownReportConfig } 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 { const { markdown, config } = props - const { styles } = config // Get frontmatter. // Get tokens. 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. let pos = 0 + const { styles, meta } = config // With default content section. const sections: ISectionOptions[] = [{ properties: { @@ -66,7 +66,7 @@ export function parseDocument(tokens: Token[], styles: IStylesOptions): Document }, }, // 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. while (pos < tokens.length) { const { tokens: section, offset } = sliceSection(tokens.slice(pos)) - sections.push(parseSection(section)) + sections.push(parseSection(section, config)) pos += offset } 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. let pos = 0 + const { meta } = config const children: (Paragraph | Table | TableOfContents)[] = [] const { content: sectionHeader = '' } = tokens[1] // Split and parse paragraphs. @@ -286,7 +287,7 @@ export function parseSection(tokens: Token[]): ISectionOptions { }, }, // TODO: replace with document title. - text: '企业实习中期报告', + text: meta.pageHeaderText, })], }), },