Skip to content

Commit 01b54d7

Browse files
committed
Refactor Turndown usage and add GFM plugin
Moved TurndownService initialization to a shared utility and integrated the @joplin/turndown-plugin-gfm for improved Markdown conversion. Updated WordImport.vue and markdown.ts to use the new utility, reducing code duplication and ensuring consistent Markdown output.
1 parent 4cdf68d commit 01b54d7

5 files changed

Lines changed: 23 additions & 10 deletions

File tree

ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@halo-dev/api-client": "^2.21.1",
2323
"@halo-dev/components": "^2.21.0",
2424
"@halo-dev/console-shared": "^2.21.0",
25+
"@joplin/turndown-plugin-gfm": "^1.0.62",
2526
"@tanstack/vue-query": "4",
2627
"@vueuse/core": "^11.3.0",
2728
"@vueuse/router": "^11.3.0",

ui/pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/src/utils/markdown.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ContentWrapper, Post } from '@halo-dev/api-client';
22
import MarkdownIt from 'markdown-it';
33
import MarkdownItAnchor from 'markdown-it-anchor';
4-
import TurndownService from 'turndown';
54
import { mergeMatter, readMatter } from './matter';
5+
import turndownService from './turndown';
66

77
export function convertPostContentToMarkdown(
88
post: Post,
@@ -13,10 +13,6 @@ export function convertPostContentToMarkdown(
1313
return content.raw || '';
1414
}
1515

16-
const turndownService = new TurndownService({
17-
headingStyle: 'atx',
18-
bulletListMarker: '-',
19-
});
2016
const rawMarkdown = turndownService.turndown(content.raw || '');
2117

2218
if (!needsFrontMatter) {

ui/src/utils/turndown.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import TurndownService from 'turndown';
2+
// @ts-ignore
3+
import { gfm } from '@joplin/turndown-plugin-gfm';
4+
5+
const turndownService = new TurndownService({
6+
headingStyle: 'atx',
7+
bulletListMarker: '-',
8+
});
9+
10+
turndownService.use(gfm);
11+
12+
export default turndownService;

ui/src/views/tabs/WordImport.vue

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts" setup>
22
import { randomUUID } from '@/utils/id';
3+
import turndownService from '@/utils/turndown';
34
import {
45
consoleApiClient,
56
ucApiClient,
@@ -17,7 +18,6 @@ import {
1718
import { useSessionStorage } from '@vueuse/core';
1819
import mammoth from 'mammoth';
1920
import PQueue from 'p-queue';
20-
import TurndownService from 'turndown';
2121
import { computed, reactive, ref } from 'vue';
2222
import MingcuteCheckCircleFill from '~icons/mingcute/check-circle-fill';
2323
import MingcuteCloseCircleLine from '~icons/mingcute/close-circle-line';
@@ -269,10 +269,6 @@ async function createPost(item: ImportItem, html: string) {
269269
};
270270
271271
if (convertToMarkdown.value) {
272-
const turndownService = new TurndownService({
273-
headingStyle: 'atx',
274-
bulletListMarker: '-',
275-
});
276272
const markdown = turndownService.turndown(html);
277273
postToCreate.content!.raw = markdown;
278274
postToCreate.content!.content = html;

0 commit comments

Comments
 (0)