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

Function htmlToMarkdownBasic

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

Source from the content-addressed store, hash-verified

294 * // Returns: '**bold** and _italic_'
295 */
296export const htmlToMarkdownBasic = (html: string): string => {
297 if (!html) {
298 return '';
299 }
300
301 const parser = new DOMParser();
302 const doc = parser.parseFromString(html, 'text/html');
303
304 const blocks = Array.from(doc.body.childNodes)
305 .map((node) => {
306 if (node.nodeType === Node.TEXT_NODE) {
307 const normalized = normalizeText(node.textContent || '').trim();
308 return normalized || null;
309 }
310
311 if (!(node instanceof Element)) {
312 return null;
313 }
314
315 const tagName = node.tagName.toLowerCase();
316
317 switch (tagName) {
318 case 'p': {
319 const content = serializeChildren(node).trim();
320 return content;
321 }
322 case 'pre': {
323 const code = node.textContent ?? '';
324 return `\`\`\`\n${normalizeText(code).trim()}\n\`\`\``;
325 }
326 case 'h1':
327 return `# ${serializeChildren(node).trim()}`;
328 case 'h2':
329 return `## ${serializeChildren(node).trim()}`;
330 case 'h3':
331 return `### ${serializeChildren(node).trim()}`;
332 case 'h4':
333 return `#### ${serializeChildren(node).trim()}`;
334 case 'h5':
335 return `##### ${serializeChildren(node).trim()}`;
336 case 'h6':
337 return `###### ${serializeChildren(node).trim()}`;
338 case 'ul':
339 return serializeList(node, false);
340 case 'ol':
341 return serializeList(node, true);
342 case 'img':
343 case 'video':
344 return serializeInline(node).trim();
345 default:
346 return serializeChildren(node).trim();
347 }
348 })
349 .filter((block): block is string => block !== null);
350
351 return blocks
352 .join('\n\n')
353 .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