fix(parser): fix parser image and fence bugs

This commit is contained in:
syy11cn 2022-05-29 16:55:30 +08:00
parent 338b8b635d
commit 7bcf33e27f
2 changed files with 25 additions and 15 deletions

View File

@ -1,4 +1,6 @@
import { readFileSync } from 'fs'
import { cwd } from 'process'
import path from 'path'
import type { IImageOptions, IRunOptions, ParagraphChild } from 'docx'
import { ImageRun, Paragraph, TextRun } from 'docx'
import type Token from 'markdown-it/lib/token'
@ -9,17 +11,18 @@ export function parseInline(props: { tokens: Token[]; style?: StyleId }): Paragr
// Variables.
const { tokens, style = StyleId.normal } = props
const { children: childrenTokens } = tokens[0]
const { length } = childrenTokens || []
if (!childrenTokens)
return new Paragraph({})
const children: ParagraphChild[] = []
let pos = 0
// Parse inline children.
while (pos < length) {
const { tokens: paragraphChild, offset: nextPos } = sliceInlineText(tokens.slice(pos))
while (pos < childrenTokens.length) {
const { tokens: paragraphChild, offset: nextPos } = sliceInlineText(childrenTokens.slice(pos))
if (paragraphChild[0].tag === 'img')
children.push(parseImage(paragraphChild))
else
children.push(parseText(paragraphChild))
pos = nextPos
pos += nextPos
}
return new Paragraph({
style,
@ -73,22 +76,24 @@ export function parseText(tokens: Token[]): TextRun {
}
export function parseImage(tokens: Token[]): ImageRun | TextRun {
const { attrGet, content } = tokens[0]
const src = attrGet('src')
const { attrs, content } = tokens[0]
const src = new Map(attrs).get('src')
const [name, resolution] = content.split('#')
const [x = 100, y = 100] = resolution.split('*')
if (!src) {
return new TextRun({
text: `[MD Report]: Image ${content} is not found.`,
text: `[MD Report]: Image ${name} is not found.`,
bold: true,
color: 'red',
highlight: 'yellow',
})
}
const options: IImageOptions = {
data: readFileSync(src).toString('base64'),
data: readFileSync(path.resolve(cwd(), src)).toString('base64'),
// TODO: Replace width and height with config in image url.
transformation: {
width: 100,
height: 100,
width: Number(x),
height: Number(y),
},
}
return new ImageRun(options)

View File

@ -1,15 +1,16 @@
import type Token from 'markdown-it/lib/token'
import { Paragraph, Table, TableCell, TableRow } from 'docx'
import { Paragraph, Table, TableCell, TableRow, TextRun } from 'docx'
import { StyleId } from '@md-report/types'
import { sliceTableRow } from './utils'
import { parseInline } from './inline'
export function parseFence(tokens: Token[]): Paragraph {
// Variables.
const { content: text } = tokens[0]
const { content } = tokens[0]
const children = content.split('\n').filter(item => item !== '').map((item, index) => new TextRun({ text: item, break: index ? 1 : 0 }))
return new Paragraph({
style: 'fence',
text,
style: StyleId.code,
children,
})
}
@ -43,9 +44,13 @@ export function parseTableRow(tokens: Token[]): TableRow {
export function parseParagraph(tokens: Token[]): Paragraph {
const inline = tokens.filter(token => token.type === 'inline')
let style = StyleId.normal
if (inline[0].children?.length === 1 && inline[0].children[0].tag === 'img')
style = StyleId.image
return parseInline({
tokens: inline,
style: StyleId.normal,
style,
})
}