From fb9ddb02c4301c78b08fe7f0437520acc0dec96f Mon Sep 17 00:00:00 2001 From: ShareVB Date: Sat, 29 Nov 2025 15:26:04 +0100 Subject: [PATCH] feat(HTML to Markdown): heading and emphasis variants --- locales/en.yml | 8 ++ .../html-to-markdown/html-to-markdown.vue | 100 ++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/locales/en.yml b/locales/en.yml index 4163eff2..7b9bafde 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -2094,6 +2094,14 @@ tools: placeholder-your-html-content: Your Html content... label-your-html-to-convert-can-paste-from-clipboard: 'Your Html to convert (can paste from clipboard):' label-output-markdown: 'Output markdown:' + label-heading-style: 'Headings Style:' + label-emphasis-style: 'Emphasis Style:' + label-hash: Hash (#) + label-underline: Underline (=/-) + label-surround-heading: Surround (= Heading =) + label-asterisk: Asterisk (*) + label-underscore: Underscore (_) + label-tilde: Tilde (~) ical-parser: title: ICAL File Parser description: Parse ICAL/ICS file to event display diff --git a/src/tools/html-to-markdown/html-to-markdown.vue b/src/tools/html-to-markdown/html-to-markdown.vue index 75c8f837..41c18152 100644 --- a/src/tools/html-to-markdown/html-to-markdown.vue +++ b/src/tools/html-to-markdown/html-to-markdown.vue @@ -3,12 +3,87 @@ import { useI18n } from 'vue-i18n'; import TurndownService from 'turndown'; import { gfm as addGFM } from '@guyplusplus/turndown-plugin-gfm'; import TextareaCopyable from '@/components/TextareaCopyable.vue'; +import { useQueryParamOrStorage } from '@/composable/queryParams'; const { t } = useI18n(); +const headingsStyle = useQueryParamOrStorage({ name: 'headings', storageName: 'html-to-md:hs', defaultValue: 'hash' }); +const emphasisStyle = useQueryParamOrStorage({ name: 'emphasis', storageName: 'html-to-md:em', defaultValue: 'standard' }); + const turndownService = new TurndownService(); + +function escapeMarkdown(text: string) { + return text + .replace(/\\/g, '\\\\') // escape backslash first + .replace(/([*_#>|`])/g, '\\$1') // escape common markdown symbols + .replace(/([\[\]\(\)])/g, '\\$1') // escape brackets and parentheses + .replace(/\|/g, '\\|'); // escape table pipes +} + +turndownService.addRule('escapeText', { + filter(node) { + return node.nodeType === 3; // text nodes + }, + replacement(content) { + return escapeMarkdown(content); + }, +}); + addGFM(turndownService); +turndownService.addRule('normalizedHeading', { + filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'], + replacement(content, node) { + const level = Number.parseInt(node.nodeName.charAt(1)); + + // Normalize spacing + const text = content.trim().replace(/\s+/g, ' '); + + // Choose style based on user preference + if (headingsStyle.value === 'underline') { + if (level === 1) { + return `\n${text}\n${'='.repeat(text.length)}\n\n`; + } + if (level === 2) { + return `\n${text}\n${'-'.repeat(text.length)}\n\n`; + } + } + else if (headingsStyle.value === 'surround') { + return `\n${'='.repeat(level)} ${text} ${'='.repeat(level)}\n\n`; + } + + // Default hash style + return `\n${'#'.repeat(level)} ${text}\n\n`; + }, +}); +// Italic rule +turndownService.addRule('customItalic', { + filter: ['em', 'i'], + replacement(content) { + if (emphasisStyle.value === 'underscore') { + return `_${content}_`; + } + if (emphasisStyle.value === 'surround') { + return `~${content}~`; + } + return `*${content}*`; + }, +}); + +// Bold rule +turndownService.addRule('customBold', { + filter: ['strong', 'b'], + replacement(content) { + if (emphasisStyle.value === 'underscore') { + return `__${content}__`; + } + if (emphasisStyle.value === 'surround') { + return `~~${content}~~`; + } + return `**${content}**`; + }, +}); + const inputHtml = ref(''); const outputMarkdown = computed(() => { try { @@ -22,6 +97,31 @@ const outputMarkdown = computed(() => {