markdown-paper 3.1.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
4
4
 
5
+ ## [3.1.1](https://github.com/LeafYeeXYZ/MarkdownPaper/compare/v3.1.0...v3.1.1) (2025-06-12)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * 修复图片嵌入问题 ([f4a0310](https://github.com/LeafYeeXYZ/MarkdownPaper/commit/f4a0310895d2640b1613bcd24e485efd657e8e85))
11
+
5
12
  ## [3.1.0](https://github.com/LeafYeeXYZ/MarkdownPaper/compare/v3.0.1...v3.1.0) (2025-06-09)
6
13
 
7
14
 
package/lib/main.ts CHANGED
@@ -1,4 +1,6 @@
1
- import fs from 'node:fs/promises'
1
+ import { readFileSync } from 'node:fs'
2
+ import { readFile, writeFile } from 'node:fs/promises'
3
+ import { dirname, extname, resolve } from 'node:path'
2
4
  import { asBlob } from 'html-docx-js-typescript'
3
5
  // @ts-ignore
4
6
  import katexCss from 'katex/dist/katex.css' with { type: 'text' }
@@ -13,9 +15,10 @@ import { type MarkdownPaperOptions, themes } from './types'
13
15
  */
14
16
  export async function render(options: MarkdownPaperOptions): Promise<void> {
15
17
  const theme = await themes[options.theme](options)
16
- const markdown = await fs.readFile(options.src, { encoding: 'utf-8' })
18
+ const markdown = await readFile(options.src, { encoding: 'utf-8' })
17
19
  marked.use(markedKatex({ throwOnError: false }))
18
- const html = await theme.preParseHtml(`
20
+ const html = (
21
+ await theme.preParseHtml(`
19
22
  <!DOCTYPE html>
20
23
  <html lang="zh-CN">
21
24
  <head>
@@ -29,6 +32,17 @@ export async function render(options: MarkdownPaperOptions): Promise<void> {
29
32
  </body>
30
33
  </html>
31
34
  `)
35
+ ).replace(/<img src="(.+?)"/g, (match, p1) => {
36
+ if (p1.startsWith('http')) return match
37
+ try {
38
+ const url = resolve(dirname(options.src), decodeURI(p1))
39
+ const data = readFileSync(url).toString('base64')
40
+ return `<img src="data:image/${extname(p1).replace('.', '')};base64,${data}"`
41
+ } catch (_) {
42
+ console.error(`图片"${p1}"不存在`)
43
+ return match
44
+ }
45
+ })
32
46
  const browser = await puppeteer.launch()
33
47
  const page = await browser.newPage()
34
48
  await page.setContent(html)
@@ -36,14 +50,14 @@ export async function render(options: MarkdownPaperOptions): Promise<void> {
36
50
  await page.pdf({ path: options.out, ...theme.pdfOptions })
37
51
  const finalHtml = await page.content()
38
52
  if (options.outputHtml) {
39
- await fs.writeFile(options.out.replace('.pdf', '.html'), finalHtml)
53
+ await writeFile(options.out.replace('.pdf', '.html'), finalHtml)
40
54
  }
41
55
  if (options.outputDocx) {
42
56
  const docx = await asBlob(finalHtml)
43
57
  const docxBuffer = new Uint8Array(
44
58
  docx instanceof Blob ? await docx.arrayBuffer() : docx.buffer,
45
59
  )
46
- await fs.writeFile(options.out.replace('.pdf', '.docx'), docxBuffer)
60
+ await writeFile(options.out.replace('.pdf', '.docx'), docxBuffer)
47
61
  console.warn('注意: 导出的Word文件可能存在格式丢失, 请手动调整')
48
62
  }
49
63
  await browser.close()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown-paper",
3
- "version": "3.1.0",
3
+ "version": "3.1.1",
4
4
  "author": {
5
5
  "name": "LeafYeeXYZ",
6
6
  "email": "xiaoyezi@leafyee.xyz"