remove the standard markdown extra new lines

This commit is contained in:
7w1 committed 2026-04-30 10:32:25 -05:00
1 parent 6aa8dde28d
commit 5c2a528229
2 files changed
+40 -9

No files matched your search

+3 -8
View File
@@ -126,7 +126,6 @@ export const toMatrixCustomHTML = (
opts: OutputOptions
): string => {
let markdownLines = '';
let insideCodeFence = false;
const parseNode = (n: Descendant, index: number, targetNodes: Descendant[]) => {
if (opts.allowBlockMarkdown && 'type' in n && n.type === BlockType.Paragraph) {
let line = toMatrixCustomHTML(n, {
@@ -136,15 +135,11 @@ export const toMatrixCustomHTML = (
});
const rawText = line.replace(/<br\/>$/, '');
const isCodeFenceLine = /^`{3}/.test(rawText);
if (isCodeFenceLine) {
insideCodeFence = !insideCodeFence;
}
// Use \n inside code fences to prevent extra blank lines from
// accumulating on each edit cycle. Normal paragraphs use \n\n.
// Use \n for all paragraphs to prevent extra blank lines from
// accumulating on each edit cycle.
line = line
.replace(/<br\/>$/, insideCodeFence || isCodeFenceLine ? '\n' : '\n\n')
.replace(/<br\/>$/, '\n')
.replace(/^(\\*)&gt;/, '$1>');
// strip nicknames if needed
+37 -1
View File
@@ -17,8 +17,44 @@ export function htmlToMarkdown(html: string): string {
return processNodes(domNodes).trim();
}
function isBlockTag(node: ChildNode | undefined): boolean {
if (!node || !isTag(node)) return false;
const blocks = [
'p',
'div',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'ul',
'ol',
'li',
'blockquote',
'pre',
'hr',
'table',
'details',
'summary',
];
return blocks.includes(node.name.toLowerCase());
}
function processNodes(nodes: ChildNode[]): string {
return nodes.map((n) => processNode(n)).join('');
return nodes
.filter((n, i) => {
if (isText(n) && /^\s*$/.test(n.data)) {
const prev = nodes[i - 1];
const next = nodes[i + 1];
// Ignore whitespace between block tags or at the edges
const isBetweenBlocks = (!prev || isBlockTag(prev)) && (!next || isBlockTag(next));
if (isBetweenBlocks) return false;
}
return true;
})
.map((n) => processNode(n))
.join('');
}
function processNode(node: ChildNode, listDepth: number = 0): string {