mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 17:41:25 -08:00
remove the standard markdown extra new lines
This commit is contained in:
2 files changed
+40
-9
No files matched your search
@@ -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(/^(\\*)>/, '$1>');
|
||||
|
||||
// strip nicknames if needed
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user