Cli: Fixes #16894: Fix quadratic conversion time for large tables (#16901)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ben BalterandClaude Opus 5.5 authored and GitHub committed 2026-10-23 12:42:49 +02:00
1 parent 451200c76c
commit 94168d33d6
3 files changed
+35 -4

No files matched your search

+26
View File
@@ -2,6 +2,7 @@ import shim from '@joplin/lib/shim';
import * as os from 'os';
import { readFile } from 'fs/promises';
import { filename } from '@joplin/lib/path-utils';
import { dirname } from 'path';
import HtmlToMd, { ParseOptions } from '@joplin/lib/HtmlToMd';
import MdToHtml from '@joplin/renderer/MdToHtml';
@@ -130,6 +131,31 @@ describe('HtmlToMd', () => {
expect(collapsedResult).toContain('\n\n');
});
it('should not re-read the rows of a table for every row', async () => {
// Under domino, each access to `table.rows` walks the whole table, so reading it once per row made
// converting large tables quadratic: https://github.com/laurent22/joplin/issues/16894
const resolveFrom = (moduleName: string, fromPath: string) => require.resolve(moduleName, { paths: [dirname(fromPath)] });
const domino: { createDocument: (html: string)=> Document } = require(resolveFrom('@mixmark-io/domino', resolveFrom('@joplin/turndown', require.resolve('@joplin/lib/HtmlToMd'))));
const rowCount = 100;
const rows = Array.from({ length: rowCount }, (_, i) => `<tr><td>${i}</td><td>b</td><td>c</td></tr>`).join('');
const root = domino.createDocument(`<div><table><thead><tr><th>A</th><th>B</th><th>C</th></tr></thead><tbody>${rows}</tbody></table></div>`).querySelector('div');
// Turndown converts a clone of the element it's given, which wouldn't have the counter below
Object.defineProperty(root, 'cloneNode', { value: () => root });
const table = root.querySelector('table');
const getRows = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(table), 'rows').get;
let rowsReadCount = 0;
Object.defineProperty(table, 'rows', { get: () => {
rowsReadCount++;
return getRows.call(table);
} });
const markdown = new HtmlToMd().parse(root);
expect(markdown.split('\n')).toHaveLength(rowCount + 2);
expect(rowsReadCount).toBeLessThan(rowCount);
});
// Covers how newlines are carried between children when joining them, which was rewritten to fix
// https://github.com/laurent22/joplin/issues/16895
it.each([
+1
View File
@@ -375,4 +375,5 @@ twodo
Gemma
untruncated
instr
mixmark
Hrvatski
+8 -4
View File
@@ -29,8 +29,11 @@ function getColumnAlignment(table, columnIndex) {
var align = '';
for (var i = 0; i < table.rows.length; ++i) {
var row = table.rows[i];
// Read `rows` once: under domino each access re-walks the whole table, which made converting large tables
// quadratic: https://github.com/laurent22/joplin/issues/16894
var rows = table.rows;
for (var i = 0; i < rows.length; ++i) {
var row = rows[i];
if (columnIndex < row.childNodes.length) {
var cellAlignment = getAlignment(row.childNodes[columnIndex]);
++votes[cellAlignment];
@@ -371,8 +374,9 @@ function handleColSpan(content, node, emptyChar) {
function tableColCount(node) {
let maxColCount = 0;
for (let i = 0; i < node.rows.length; i++) {
const row = node.rows[i]
const rows = node.rows;
for (let i = 0; i < rows.length; i++) {
const row = rows[i]
const colCount = row.childNodes.length
if (colCount > maxColCount) maxColCount = colCount
}