fix: make markdown *actually* render single newlines as double newlines
This commit is contained in:
@@ -10,8 +10,58 @@ const props = defineProps<{
|
||||
|
||||
const { $remark } = useNuxtApp();
|
||||
|
||||
// this function effectively removes lazy concetenation from the markdown
|
||||
// normally in markdown, if you have two lines that are separated by only
|
||||
// one new line. E.g.:
|
||||
//
|
||||
// This is some markdown text but I split
|
||||
// it into two lines so it's easier to read in the editor
|
||||
//
|
||||
// they are *usually* concatenated into one line, however,
|
||||
// since we are a web editor, with line wrapping, that behavior
|
||||
// in undesirable, so pre preserve newlines by just maiking
|
||||
// every new line two newlines (as long as we arent in a codeblock)
|
||||
function preprocessMarkdown(doc: string) {
|
||||
const lines = doc.split('\n');
|
||||
let inCode = false;
|
||||
let result: string[] = [];
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const fullLine = lines[i]!;
|
||||
|
||||
const match = fullLine.match(/^( {0,3})(.*)/);
|
||||
const content = match?.[2] || '';
|
||||
|
||||
if (content.startsWith('```')) {
|
||||
if (!inCode) {
|
||||
inCode = true;
|
||||
} else {
|
||||
inCode = false;
|
||||
}
|
||||
result.push(fullLine);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inCode) {
|
||||
result.push(fullLine);
|
||||
} else {
|
||||
if (content.trim().length === 0) {
|
||||
result.push('');
|
||||
} else {
|
||||
if (result.length > 0 && result[result.length - 1] !== '') {
|
||||
result.push('');
|
||||
}
|
||||
result.push(fullLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.join('\n');
|
||||
}
|
||||
|
||||
|
||||
const ast = computed(() => {
|
||||
const mdast = $remark.parse(props.content);
|
||||
const mdast = $remark.parse(preprocessMarkdown(props.content));
|
||||
return $remark.runSync(mdast);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user