diff --git a/.changeset/fix-arbitrary-list-starts.md b/.changeset/fix-arbitrary-list-starts.md
new file mode 100644
index 000000000..4e89c16be
--- /dev/null
+++ b/.changeset/fix-arbitrary-list-starts.md
@@ -0,0 +1,5 @@
+---
+default: patch
+---
+
+Fixed starting lists at arbitrary numbers.
diff --git a/src/app/plugins/markdown/bidirectional.test.ts b/src/app/plugins/markdown/bidirectional.test.ts
index 56c036121..a987ab15e 100644
--- a/src/app/plugins/markdown/bidirectional.test.ts
+++ b/src/app/plugins/markdown/bidirectional.test.ts
@@ -94,7 +94,6 @@ describe('bidirectional round-trip', () => {
const html = markdownToHtml(markdown);
const injected = injectDataMd(html);
const result = htmlToMarkdown(injected);
- // Note: marked normalizes ordered lists to start at 1, but we increment for output
expect(result).toContain('1. First');
expect(result).toContain('2. Second');
});
diff --git a/src/app/plugins/markdown/markdownToHtml.test.ts b/src/app/plugins/markdown/markdownToHtml.test.ts
index fcecdaed1..f6599622a 100644
--- a/src/app/plugins/markdown/markdownToHtml.test.ts
+++ b/src/app/plugins/markdown/markdownToHtml.test.ts
@@ -216,6 +216,17 @@ describe('markdownToHtml', () => {
expect(result).not.toContain('
');
});
+ it('preserves arbitrary ordered list start numbers', () => {
+ const result = markdownToHtml('23423. hello');
+ expect(result).toContain('start="23423"');
+ });
+
+ it('strips invalid ol start values', () => {
+ const result = markdownToHtml('- x
');
+ expect(result).toContain('');
+ expect(result).not.toContain('start=');
+ });
+
it('handles text without markdown', () => {
const result = markdownToHtml('Plain text without any formatting');
expect(result).toContain('Plain text');
diff --git a/src/app/plugins/markdown/markdownToHtml.ts b/src/app/plugins/markdown/markdownToHtml.ts
index dded0509c..c7761859e 100644
--- a/src/app/plugins/markdown/markdownToHtml.ts
+++ b/src/app/plugins/markdown/markdownToHtml.ts
@@ -56,6 +56,8 @@ const decodeHtmlEntities = (text: string): string => {
const MATRIX_TO_PLACEHOLDER_PREFIX = 'MATRIXTORAWLINKTOKEN';
+const ORDERED_LIST_START_REGEX = /^-?\d+$/;
+
const escapeHtml = (text: string): string =>
text
.replace(/&/g, '&')
@@ -152,12 +154,18 @@ export function markdownToHtml(markdown: string, options?: MarkdownToHtmlOptions
const allowlistedHtml = escapeNonAllowlistedHtmlTags(unescapedInline);
- // Force all links to open in a new tab
+ // Force all links to open in a new tab, validate .
DOMPurify.addHook('afterSanitizeAttributes', (node) => {
if (node.tagName === 'A' && node.getAttribute('href')) {
node.setAttribute('target', '_blank');
node.setAttribute('rel', 'noreferrer noopener');
}
+ if (node.tagName === 'OL') {
+ const start = node.getAttribute('start');
+ if (start !== null && !ORDERED_LIST_START_REGEX.test(start)) {
+ node.removeAttribute('start');
+ }
+ }
});
const sanitized = DOMPurify.sanitize(allowlistedHtml, {
@@ -185,7 +193,9 @@ export function markdownToHtml(markdown: string, options?: MarkdownToHtmlOptions
],
// Ensure these safe attrs survive sanitization even when the input HTML
// originates from markdown-embedded tags (e.g. custom emoji
).
+ // `start` must be URI-safe or DOMPurify drops it when ALLOWED_URI_REGEXP is set.
ADD_ATTR: ['target', 'rel', 'height', 'width'],
+ ADD_URI_SAFE_ATTR: ['start'],
// Force all links to have safe rel attribute
FORCE_BODY: false,
ALLOWED_URI_REGEXP: /^(?:https?|ftp|mailto|magnet|mxc):/i,
diff --git a/src/app/utils/sanitize.test.ts b/src/app/utils/sanitize.test.ts
index c1332cfc7..b4839f926 100644
--- a/src/app/utils/sanitize.test.ts
+++ b/src/app/utils/sanitize.test.ts
@@ -122,6 +122,14 @@ describe('sanitizeCustomHtml', () => {
expect(result.match(/\ssrc=/g)).toHaveLength(1);
});
+ it('drops invalid ol start values', () => {
+ expect(sanitizeCustomHtml('- ok
')).toContain('start="2"');
+ expect(sanitizeCustomHtml('- x
')).not.toContain(
+ 'start='
+ );
+ expect(sanitizeCustomHtml('- x
')).not.toContain('start=');
+ });
+
it('drops invalid Matrix color attributes instead of translating them to style', () => {
const result = sanitizeCustomHtml(
'text'