mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
fix: nested list indentation
This commit is contained in:
1 parent
b0349b431c
commit
ee575c0dbd
6 files changed
+95
-2
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Fixed nested lists having wrong indentation levels when editing.
|
||||
@@ -103,6 +103,26 @@ describe('bidirectional round-trip', () => {
|
||||
expect(result).toContain('2. Second');
|
||||
});
|
||||
|
||||
it('round-trips nested sublists with four-space indent', () => {
|
||||
const markdown = '1. parent\n - child';
|
||||
const html = markdownToHtml(markdown);
|
||||
expect(html).toMatch(/<li>[\s\S]*<ul/i);
|
||||
const injected = injectDataMd(html);
|
||||
const result = htmlToMarkdown(injected);
|
||||
expect(result).toContain('1. parent');
|
||||
expect(result).toContain(' - child');
|
||||
});
|
||||
|
||||
it('round-trips nested sublists written with two-space indent', () => {
|
||||
const markdown = '1. parent\n - child';
|
||||
const html = markdownToHtml(markdown);
|
||||
expect(html).toMatch(/<li>[\s\S]*<ul/i);
|
||||
const injected = injectDataMd(html);
|
||||
const result = htmlToMarkdown(injected);
|
||||
expect(result).toContain('1. parent');
|
||||
expect(result).toContain('- child');
|
||||
});
|
||||
|
||||
it('round-trips spoiler syntax', () => {
|
||||
const markdown = '||hidden message||';
|
||||
const html = markdownToHtml(markdown);
|
||||
|
||||
@@ -15,6 +15,24 @@ describe('expandBlockBoundariesAfterSingleNewlines', () => {
|
||||
it('still expands when a blockquote ends', () => {
|
||||
expect(expandBlockBoundariesAfterSingleNewlines('> quote\nplain')).toBe('> quote\n\nplain');
|
||||
});
|
||||
|
||||
it('does not expand between consecutive ordered list items', () => {
|
||||
expect(expandBlockBoundariesAfterSingleNewlines('1. one\n2. two')).toBe('1. one\n2. two');
|
||||
});
|
||||
|
||||
it('does not expand before a 2-space nested sublist', () => {
|
||||
expect(expandBlockBoundariesAfterSingleNewlines('1. test\n - sub')).toBe('1. test\n - sub');
|
||||
});
|
||||
|
||||
it('does not expand before a 4-space nested sublist', () => {
|
||||
expect(expandBlockBoundariesAfterSingleNewlines('1. test\n - sub')).toBe(
|
||||
'1. test\n - sub'
|
||||
);
|
||||
});
|
||||
|
||||
it('still expands before the first top-level list item after prose', () => {
|
||||
expect(expandBlockBoundariesAfterSingleNewlines('intro\n- item')).toBe('intro\n\n- item');
|
||||
});
|
||||
});
|
||||
|
||||
describe('consecutive blockquotes', () => {
|
||||
|
||||
@@ -135,11 +135,41 @@ function nextLineIsBlockStarter(md: string, newlineIdx: number): boolean {
|
||||
return looksLikeBlockStart(effective);
|
||||
}
|
||||
|
||||
function leadingSpaces(line: string): number {
|
||||
let k = 0;
|
||||
while (k < line.length && line[k] === ' ') k++;
|
||||
return k;
|
||||
}
|
||||
|
||||
function lineLooksLikeListItem(line: string): boolean {
|
||||
const effective = effectiveContentAfterEscapes(line);
|
||||
if (effective === null) return false;
|
||||
return /^\d{1,9}\.\s/.test(effective) || /^[-*+]\s/.test(effective);
|
||||
}
|
||||
|
||||
function nextLineIsNestedListItem(md: string, newlineIdx: number): boolean {
|
||||
const prevLine = lineAtNewline(md, newlineIdx);
|
||||
const nextLine = lineAfterNewline(md, newlineIdx);
|
||||
if (!lineLooksLikeListItem(nextLine)) return false;
|
||||
return leadingSpaces(nextLine) > leadingSpaces(prevLine);
|
||||
}
|
||||
|
||||
function nextLineIsSiblingListItem(md: string, newlineIdx: number): boolean {
|
||||
const prevLine = lineAtNewline(md, newlineIdx);
|
||||
const nextLine = lineAfterNewline(md, newlineIdx);
|
||||
if (!lineLooksLikeListItem(prevLine) || !lineLooksLikeListItem(nextLine)) return false;
|
||||
return leadingSpaces(prevLine) === leadingSpaces(nextLine);
|
||||
}
|
||||
|
||||
function shouldExpandSingleNewline(md: string, newlineIdx: number): boolean {
|
||||
// Consecutive `>` lines belong to one blockquote, keep the single `\n` between them.
|
||||
if (prevLineIsBlockquote(md, newlineIdx) && nextLineContinuesBlockquote(md, newlineIdx)) {
|
||||
return false;
|
||||
}
|
||||
// Keep single `\n` between list items (siblings) and before nested sublists (2- or 4-space indent).
|
||||
if (nextLineIsSiblingListItem(md, newlineIdx) || nextLineIsNestedListItem(md, newlineIdx)) {
|
||||
return false;
|
||||
}
|
||||
if (nextLineIsBlockStarter(md, newlineIdx)) return true;
|
||||
// CommonMark lazy continuation keeps non-`>` lines inside blockquotes, close on single `\n`.
|
||||
if (prevLineIsBlockquote(md, newlineIdx) && !nextLineContinuesBlockquote(md, newlineIdx)) {
|
||||
|
||||
@@ -4,9 +4,12 @@ import {
|
||||
MX_EMOTICON_MD_SEP,
|
||||
MX_EMOTICON_MD_START,
|
||||
} from './extensions/matrix-emoticon';
|
||||
import { toMatrixCustomHTML, trimCustomHtml } from '$components/editor/output';
|
||||
import { plainToEditorInput } from '$components/editor/input';
|
||||
import { BlockType } from '$components/editor/types';
|
||||
import { injectDataMd } from './injectDataMd';
|
||||
import { htmlToMarkdown } from './htmlToMarkdown';
|
||||
import { markdownToHtml } from './markdownToHtml';
|
||||
|
||||
describe('htmlToMarkdown', () => {
|
||||
it('converts headings', () => {
|
||||
@@ -125,6 +128,20 @@ describe('htmlToMarkdown', () => {
|
||||
expect(result).toContain('Item 1');
|
||||
});
|
||||
|
||||
it('indents nested sublists with four spaces per level', () => {
|
||||
const html = '<ol><li><p>parent</p><ul><li><p>child</p></li></ul></li></ol>';
|
||||
expect(htmlToMarkdown(html)).toContain('1. parent');
|
||||
expect(htmlToMarkdown(html)).toContain(' - child');
|
||||
});
|
||||
|
||||
it('edit-load and save round-trip keeps nested sublist', () => {
|
||||
const md = '1. test\n - sub\n';
|
||||
const loaded = htmlToMarkdown(injectDataMd(markdownToHtml(md)));
|
||||
expect(loaded).toContain(' - sub');
|
||||
const html = trimCustomHtml(toMatrixCustomHTML(plainToEditorInput(loaded), {}));
|
||||
expect(html).toMatch(/<li>[\s\S]*<ul/i);
|
||||
});
|
||||
|
||||
it('preserves data-md attributes for round-trip', () => {
|
||||
const result = htmlToMarkdown('<strong data-md="**">bold</strong>');
|
||||
expect(result).toContain('**bold**');
|
||||
|
||||
@@ -11,6 +11,9 @@ import { isAllowedHtmlTag } from './allowedHtmlTags';
|
||||
import { formatMfmColorDataMd } from './extensions/matrix-mfm-color';
|
||||
import { isMatrixHexColor } from '$utils/matrixHtml';
|
||||
|
||||
/** CommonMark list nesting indent (four spaces per level). */
|
||||
const LIST_MARKDOWN_INDENT = ' ';
|
||||
|
||||
/**
|
||||
* Converts Matrix-compatible HTML back to markdown for round-trip editing.
|
||||
* Preserves original markdown syntax via data-md attributes and converts
|
||||
@@ -419,7 +422,7 @@ function processUnorderedList(
|
||||
insideCode: boolean = false
|
||||
): string {
|
||||
const mdSequence = node.attribs['data-md'] || '-';
|
||||
const indent = ' '.repeat(depth);
|
||||
const indent = LIST_MARKDOWN_INDENT.repeat(depth);
|
||||
const items = node.children
|
||||
.filter((c): c is Element => isTag(c) && c.name === 'li')
|
||||
.map((li) => {
|
||||
@@ -439,7 +442,7 @@ function processOrderedList(node: Element, depth: number = 0, insideCode: boolea
|
||||
? mdSequence
|
||||
: `${mdSequence}.`;
|
||||
|
||||
const indent = ' '.repeat(depth);
|
||||
const indent = LIST_MARKDOWN_INDENT.repeat(depth);
|
||||
const items = node.children
|
||||
.filter((c): c is Element => isTag(c) && c.name === 'li')
|
||||
.map((li, index) => {
|
||||
|
||||
Reference in new issue
Block a user