This commit is contained in:
Sidharth Vinod
2023-04-26 11:25:22 +05:30
parent 3c9b169ac2
commit ba2c076ead
3 changed files with 9 additions and 90 deletions

View File

@@ -1,85 +0,0 @@
import { fromMarkdown } from 'mdast-util-from-markdown';
import { dedent } from 'ts-dedent';
/**
* @param {string} markdown markdown to process
* @returns {string} processed markdown
*/
function preprocessMarkdown(markdown) {
// Replace multiple newlines with a single newline
const withoutMultipleNewlines = markdown.replace(/\n{2,}/g, '\n');
// Remove extra spaces at the beginning of each line
const withoutExtraSpaces = dedent(withoutMultipleNewlines);
return withoutExtraSpaces;
}
/**
* @param {string} markdown markdown to split into lines
*/
export function markdownToLines(markdown) {
const preprocessedMarkdown = preprocessMarkdown(markdown);
const { children } = fromMarkdown(preprocessedMarkdown);
const lines = [[]];
let currentLine = 0;
/**
* @param {import('mdast').Content} node
* @param {string} [parentType]
*/
function processNode(node, parentType = 'normal') {
if (node.type === 'text') {
const textLines = node.value.split('\n');
textLines.forEach((textLine, index) => {
if (index !== 0) {
currentLine++;
lines.push([]);
}
textLine.split(' ').forEach((word) => {
if (word) {
lines[currentLine].push({ content: word, type: parentType });
}
});
});
} else if (node.type === 'strong' || node.type === 'emphasis') {
node.children.forEach((contentNode) => {
processNode(contentNode, node.type);
});
}
}
children.forEach((treeNode) => {
if (treeNode.type === 'paragraph') {
treeNode.children.forEach((contentNode) => {
processNode(contentNode);
});
}
});
return lines;
}
/**
* @param {string} markdown markdown to convert to HTML
* @returns {string} HTML
*/
export function markdownToHTML(markdown) {
const { children } = fromMarkdown(markdown);
/**
* @param {import('mdast').Content} node
*/
function output(node) {
if (node.type === 'text') {
return node.value.replace(/\n/g, '<br/>');
} else if (node.type === 'strong') {
return `<strong>${node.children.map(output).join('')}</strong>`;
} else if (node.type === 'emphasis') {
return `<em>${node.children.map(output).join('')}</em>`;
} else if (node.type === 'paragraph') {
return `<p>${node.children.map(output).join('')}</p>`;
}
return `Unsupported markdown: ${node.type}`;
}
return children.map(output).join('');
}

View File

@@ -237,10 +237,13 @@ test('markdownToHTML - Only italic formatting', () => {
}); });
test('markdownToHTML - Mixed formatting', () => { test('markdownToHTML - Mixed formatting', () => {
const input = `*Italic* and **bold** formatting`; expect(markdownToHTML(`*Italic* and **bold** formatting`)).toMatchInlineSnapshot(
const expectedOutput = `<p><em>Italic</em> and <strong>bold</strong> formatting</p>`; '"<p><em>Italic</em> and <strong>bold</strong> formatting</p>"'
const output = markdownToHTML(input); );
expect(output).toEqual(expectedOutput);
expect(markdownToHTML('special characters: `<p>hi</p>`')).toMatchInlineSnapshot(
'"<p>special characters: <code>&lt;p&gt;hi&lt;/p&gt;</code></p>"'
);
}); });
test('markdownToHTML - Unsupported formatting', () => { test('markdownToHTML - Unsupported formatting', () => {

View File

@@ -1,12 +1,13 @@
import { micromark } from 'micromark'; import { micromark } from 'micromark';
import { fromMarkdown } from 'mdast-util-from-markdown'; import { fromMarkdown } from 'mdast-util-from-markdown';
import type { Content } from 'mdast'; import type { Content } from 'mdast';
import { dedent } from 'ts-dedent';
function preprocessMarkdown(markdown: string): string { function preprocessMarkdown(markdown: string): string {
// Replace multiple newlines with a single newline // Replace multiple newlines with a single newline
const withoutMultipleNewlines = markdown.replace(/\n{2,}/g, '\n'); const withoutMultipleNewlines = markdown.replace(/\n{2,}/g, '\n');
// Remove extra spaces at the beginning of each line // Remove extra spaces at the beginning of each line
const withoutExtraSpaces = withoutMultipleNewlines.replace(/^\s+/gm, ''); const withoutExtraSpaces = dedent(withoutMultipleNewlines);
return withoutExtraSpaces; return withoutExtraSpaces;
} }