Skip to content

Commit f06ba11

Browse files
committed
fix: improve turndown config for GFM support and custom styles
1 parent f35d68d commit f06ba11

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"terra-draw": "^1.18.1",
7272
"tributejs": "^5.1.3",
7373
"turndown": "^7.2.4",
74+
"turndown-plugin-gfm": "^1.0.2",
7475
"use-query-params": "^2.2.1",
7576
"webfontloader": "^1.6.28",
7677
"workbox-core": "^7.0.0",

frontend/src/utils/htmlFromMarkdown.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { marked } from 'marked';
22
import DOMPurify from 'dompurify';
33
import TurndownService from 'turndown';
4+
import { gfm } from 'turndown-plugin-gfm';
45

56
const VIDEO_TAG_REGEXP = new RegExp(/^::youtube\[(.*)\]$/);
67

@@ -104,7 +105,37 @@ export const formatUserNamesToLink = (text) => {
104105
return text;
105106
};
106107

107-
const turndownService = new TurndownService();
108+
const turndownService = new TurndownService({
109+
headingStyle: 'atx',
110+
codeBlockStyle: 'fenced',
111+
hr: '---',
112+
bullet: '-',
113+
});
114+
turndownService.use(gfm);
115+
116+
// Rule to convert YouTube iframes back to ::youtube[id] syntax
117+
turndownService.addRule('youtube', {
118+
filter: (node) => {
119+
return (
120+
node.nodeName === 'IFRAME' &&
121+
node.getAttribute('src')?.startsWith('https://www.youtube.com/embed/')
122+
);
123+
},
124+
replacement: (content, node) => {
125+
const src = node.getAttribute('src');
126+
const id = src.split('/').pop();
127+
return `\n\n::youtube[${id}]\n\n`;
128+
},
129+
});
130+
131+
// Rule to use double tildes for strikethrough
132+
turndownService.addRule('strikethrough', {
133+
filter: ['del', 's', 'strike'],
134+
replacement: (content) => `~~${content}~~`,
135+
});
136+
137+
// Rule to keep other iframes
138+
turndownService.keep(['iframe']);
108139

109140
export const markdownFromHtml = (html) => {
110141
return turndownService.turndown(html);

frontend/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14107,6 +14107,11 @@ tsutils@^3.21.0:
1410714107
dependencies:
1410814108
tslib "^1.8.1"
1410914109

14110+
turndown-plugin-gfm@^1.0.2:
14111+
version "1.0.2"
14112+
resolved "https://registry.yarnpkg.com/turndown-plugin-gfm/-/turndown-plugin-gfm-1.0.2.tgz#6f8678a361f35220b2bdf5619e6049add75bf1c7"
14113+
integrity sha512-vwz9tfvF7XN/jE0dGoBei3FXWuvll78ohzCZQuOb+ZjWrs3a0XhQVomJEb2Qh4VHTPNRO4GPZh0V7VRbiWwkRg==
14114+
1411014115
turndown@^7.2.4:
1411114116
version "7.2.4"
1411214117
resolved "https://registry.yarnpkg.com/turndown/-/turndown-7.2.4.tgz#42d98202aefa8c188c997b586bc6da78bdf27ea2"

0 commit comments

Comments
 (0)