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

Function htmlToMarkdownBasic

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

Source from the content-addressed store, hash-verified

323 * // Returns: '**bold** and _italic_'
324 */
325export const htmlToMarkdownBasic = (html: string): string => {
326 if (!html) {
327 return '';
328 }
329
330 const parser = new DOMParser();
331 const doc = parser.parseFromString(html, 'text/html');
332
333 const blocks = Array.from(doc.body.childNodes)
334 .map((node) => {
335 if (node.nodeType === Node.TEXT_NODE) {
336 const normalized = normalizeText(node.textContent || '').trim();
337 return normalized || null;
338 }
339
340 if (!(node instanceof Element)) {
341 return null;
342 }
343
344 const tagName = node.tagName.toLowerCase();
345
346 switch (tagName) {
347 case 'p': {
348 const content = serializeChildren(node).trim();
349 return content;
350 }
351 case 'pre': {
352 const code = node.textContent ?? '';
353 return `\`\`\`\n${normalizeText(code).trim()}\n\`\`\``;
354 }
355 case 'h1':
356 return `# ${serializeChildren(node).trim()}`;
357 case 'h2':
358 return `## ${serializeChildren(node).trim()}`;
359 case 'h3':
360 return `### ${serializeChildren(node).trim()}`;
361 case 'h4':
362 return `#### ${serializeChildren(node).trim()}`;
363 case 'h5':
364 return `##### ${serializeChildren(node).trim()}`;
365 case 'h6':
366 return `###### ${serializeChildren(node).trim()}`;
367 case 'ul':
368 return serializeList(node, false);
369 case 'ol':
370 return serializeList(node, true);
371 case 'img':
372 case 'video':
373 return serializeInline(node).trim();
374 default:
375 return serializeChildren(node).trim();
376 }
377 })
378 .filter((block): block is string => block !== null);
379
380 return blocks
381 .join('\n\n')
382 .replace(/\n{4,}/g, '\n\n\n')

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