MCPcopy Create free account
hub / github.com/dailydotdev/apps / htmlToMarkdownBasic

Function htmlToMarkdownBasic

packages/shared/src/lib/markdownConversion.ts:279–337  ·  view source on GitHub ↗
(html: string)

Source from the content-addressed store, hash-verified

277 * // Returns: '**bold** and _italic_'
278 */
279export const htmlToMarkdownBasic = (html: string): string => {
280 if (!html) {
281 return '';
282 }
283
284 const parser = new DOMParser();
285 const doc = parser.parseFromString(html, 'text/html');
286
287 const blocks = Array.from(doc.body.childNodes)
288 .map((node) => {
289 if (node.nodeType === Node.TEXT_NODE) {
290 const normalized = normalizeText(node.textContent || '').trim();
291 return normalized || null;
292 }
293
294 if (!(node instanceof Element)) {
295 return null;
296 }
297
298 const tagName = node.tagName.toLowerCase();
299
300 switch (tagName) {
301 case 'p': {
302 const content = serializeChildren(node).trim();
303 return content;
304 }
305 case 'pre': {
306 const code = node.textContent ?? '';
307 return `\`\`\`\n${normalizeText(code).trim()}\n\`\`\``;
308 }
309 case 'h1':
310 return `# ${serializeChildren(node).trim()}`;
311 case 'h2':
312 return `## ${serializeChildren(node).trim()}`;
313 case 'h3':
314 return `### ${serializeChildren(node).trim()}`;
315 case 'h4':
316 return `#### ${serializeChildren(node).trim()}`;
317 case 'h5':
318 return `##### ${serializeChildren(node).trim()}`;
319 case 'h6':
320 return `###### ${serializeChildren(node).trim()}`;
321 case 'ul':
322 return serializeList(node, false);
323 case 'ol':
324 return serializeList(node, true);
325 case 'img':
326 return serializeInline(node).trim();
327 default:
328 return serializeChildren(node).trim();
329 }
330 })
331 .filter((block): block is string => block !== null);
332
333 return blocks
334 .join('\n\n')
335 .replace(/\n{4,}/g, '\n\n\n')
336 .trim();

Callers 2

RichTextInputFunction · 0.90

Calls 5

serializeChildrenFunction · 0.85
serializeListFunction · 0.85
serializeInlineFunction · 0.85
replaceMethod · 0.80
normalizeTextFunction · 0.70

Tested by

no test coverage detected