-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdUtils.js
55 lines (43 loc) · 1.9 KB
/
mdUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkRehype from 'remark-rehype'
import rehypeHighlight from 'rehype-highlight';
import rehypeFormat from 'rehype-format'
import rehypeStringify from 'rehype-stringify';
export function convertMarkdownCodeblockToHtml (markdownText, indentation = null) {
const processor = unified()
.use(remarkParse) // Parse Markdown
.use(remarkRehype) // Convert Markdown to HTML
.use(rehypeHighlight) // Highlight code blocks
.use(rehypeFormat) // Formats HTML
.use(rehypeStringify) // Convert HTML back to string
const htmlContent = processor.processSync(markdownText)
let htmlContentStr = String(htmlContent)
// console.log(htmlContentStr)
if (indentation) {
htmlContentStr = replaceTextWithIndentation(htmlContentStr, indentation)
}
// Replaces between lines
const codeTagRegex = /<pre>.*<code[^>]*>\n/g
const codeTagRegex2 = /\n *<\/code>.*<\/pre>/g
htmlContentStr = htmlContentStr.replace(codeTagRegex, (match) => match.trim())
htmlContentStr = htmlContentStr.replace(codeTagRegex2, (match) => match.trim())
return htmlContentStr
}
export function replaceTextWithIndentation(originalText, indentation) {
// Split the original text into lines
const lines = originalText.split('\n');
// Replace the text and maintain indentation
const replacedLines = lines.map(line => {
return line.replace(line, indentation + line);
});
// Join the lines back into a single string
let replacedText = replacedLines.join('\n');
const codeTagRegex = /(<pre>.*<code[^>]*>)/g
const firstSpanWithIndentationRegex = /( {2,})(?=<span[^>]*>)/
const spanIndentation = replacedText.match(firstSpanWithIndentationRegex)
if (spanIndentation) {
replacedText = replacedText.replace(codeTagRegex, `$1\n`)
}
return replacedText
}