bullet proof :)

This commit is contained in:
lovebird 2026-03-02 23:03:52 +01:00
parent 49c1a607d3
commit 0ccfc6414f
7 changed files with 156 additions and 0 deletions

7
dist/ast/mdast.js vendored
View File

@ -23,11 +23,18 @@ function getMdast(markdown) {
function getMarkdown(mdast) {
return toMarkdown(mdast, {
extensions: [frontmatterToMarkdown("yaml"), mdxToMarkdown(), gfmTableToMarkdown(), htmlCommentToMarkdown()],
listItemIndent: "one",
join: [
(__, _, parent) => {
if (mdNodeIsJsxElement(parent)) {
return 0;
}
if (mdNodeIs(parent, "list")) {
return 0;
}
if (mdNodeIs(parent, "listItem")) {
return 0;
}
return 1;
}
]

38
experiments/test-fix.mjs Normal file
View File

@ -0,0 +1,38 @@
import prettier from 'prettier';
import { fromMarkdown } from 'mdast-util-from-markdown';
import { toMarkdown } from 'mdast-util-to-markdown';
const md = `The panel includes the following settings:
* ON/OFF Toggle: A main switch.
* Min Heating Time (1-60s): Minimum duration.
* Mode: Selects the sequential heating algorithm:
* 0 - All: Cycles through all devices with time-based control.
* 1 - SP: Cycles through devices in groups.
* 2 - SP Any: Heats any devices that need heating.
* Post-Heatup Mode: Mode to switch to after initial heatup phase.
* Current Status: Display field showing the current state.`;
const prettified = await prettier.format(md, {
parser: 'mdx',
printWidth: Infinity,
proseWrap: 'never',
useTabs: true
});
const tree = fromMarkdown(prettified);
// FIX: also handle listItem parent in the join rule
const result = toMarkdown(tree, {
listItemIndent: 'one',
join: [
(__, _, parent) => {
if (parent?.type === 'list') return 0;
if (parent?.type === 'listItem') return 0;
return 1;
}
]
});
console.log('=== FIXED output ===');
console.log(result);

View File

@ -0,0 +1,51 @@
import prettier from 'prettier';
import { fromMarkdown } from 'mdast-util-from-markdown';
import { toMarkdown } from 'mdast-util-to-markdown';
const md = `The panel includes the following settings:
* ON/OFF Toggle: A main switch.
* Min Heating Time (1-60s): Minimum duration.
* Mode: Selects the sequential heating algorithm:
* 0 - All: Cycles through all devices with time-based control.
* 1 - SP: Cycles through devices in groups.
* 2 - SP Any: Heats any devices that need heating.
* Post-Heatup Mode: Mode to switch to after initial heatup phase.
* Current Status: Display field showing the current state.`;
console.log('=== Input ===');
console.log(md);
const prettified = await prettier.format(md, {
parser: 'mdx',
printWidth: Infinity,
proseWrap: 'never',
useTabs: true
});
console.log('\n=== After Prettier ===');
console.log(JSON.stringify(prettified));
console.log(prettified);
const tree = fromMarkdown(prettified);
const list = tree.children[1]; // after the paragraph
console.log('=== AST Analysis of list ===');
console.log('list spread:', list.spread);
for (const [i, item] of list.children.entries()) {
console.log(`item[${i}] spread:`, item.spread, 'children:', item.children.length, item.children.map(c => c.type));
}
// Now serialize with the join rule:
const result = toMarkdown(tree, {
listItemIndent: 'one',
join: [
(__, _, parent) => {
if (parent?.type === 'list') return 0;
return 1;
}
]
});
console.log('\n=== Serialized output ===');
console.log(result);

View File

@ -0,0 +1,50 @@
import { fromMarkdown } from 'mdast-util-from-markdown';
import { toMarkdown } from 'mdast-util-to-markdown';
const md = `* Mode: Selects the algorithm:
* 0 - All: Cycles through all devices.
* 1 - SP: Cycles through devices in groups.
* Post-Mode: blah`;
const tree = fromMarkdown(md);
const list = tree.children[0];
console.log('=== AST Analysis ===');
console.log('list spread:', list.spread);
for (const [i, item] of list.children.entries()) {
console.log(`item[${i}] spread:`, item.spread, 'children count:', item.children.length, 'children types:', item.children.map(c => c.type));
}
console.log('\n=== Default toMarkdown ===');
console.log(JSON.stringify(toMarkdown(tree)));
console.log('\n=== With join rule ===');
const result = toMarkdown(tree, {
listItemIndent: 'one',
join: [
(__, _, parent) => {
if (parent?.type === 'list') return 0;
if (parent?.type === 'listItem') return 0;
return 1;
}
]
});
console.log(JSON.stringify(result));
console.log('\n=== With join rule + forced spread=false ===');
function clearSpread(node) {
if (node.spread !== undefined) node.spread = false;
if (node.children) node.children.forEach(clearSpread);
}
clearSpread(tree);
const result2 = toMarkdown(tree, {
listItemIndent: 'one',
join: [
(__, _, parent) => {
if (parent?.type === 'list') return 0;
if (parent?.type === 'listItem') return 0;
return 1;
}
]
});
console.log(JSON.stringify(result2));

View File

@ -18,6 +18,7 @@
},
"scripts": {
"build": "tsc && node build.js",
"dev": "tsc -w & node --watch build.js",
"test": "vitest run --reporter verbose",
"test:watch": "vitest watch --reporter verbose",
"test:tables": "vitest run src/__test__/e2e.test.ts --reporter verbose"

0
src/.gitignore vendored
View File

View File

@ -153,12 +153,21 @@ export function getMdast(markdown: string): MdRoot {
export function getMarkdown(mdast: MdRoot): string {
return toMarkdown(mdast, {
extensions: [frontmatterToMarkdown('yaml'), mdxToMarkdown(), gfmTableToMarkdown(), htmlCommentToMarkdown()],
listItemIndent: 'one',
join: [
(__, _, parent) => {
if (mdNodeIsJsxElement(parent)) {
return 0;
}
// Keep list items tight (no blank lines between them)
if (mdNodeIs(parent, 'list')) {
return 0;
}
// Keep content within a list item tight (e.g. paragraph + nested sub-list)
if (mdNodeIs(parent, 'listItem')) {
return 0;
}
return 1;
}
]