@mdream/js 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -85,6 +85,73 @@ function shouldAddSpacingBeforeText(lastChar, lastNode, textNode) {
85
85
  if (firstChar === "." || firstChar === "," || firstChar === "!" || firstChar === "?" || firstChar === ":" || firstChar === ";" || firstChar === "_" || firstChar === "*" || firstChar === "`" || firstChar === ")" || firstChar === "]") return false;
86
86
  return true;
87
87
  }
88
+ function canWrapHere(depthMap) {
89
+ if (depthMap[34] || depthMap[23] || depthMap[32] || depthMap[31]) return false;
90
+ for (let h = 7; h <= 12; h++) if (depthMap[h]) return false;
91
+ return true;
92
+ }
93
+ function currentColumn(buffer) {
94
+ let col = 0;
95
+ for (let i = buffer.length - 1; i >= 0; i--) {
96
+ const s = buffer[i];
97
+ const nl = s.lastIndexOf("\n");
98
+ if (nl >= 0) return col + [...s.slice(nl + 1)].length;
99
+ col += [...s].length;
100
+ }
101
+ return col;
102
+ }
103
+ function wrapContinuationPrefix(state, node) {
104
+ const chain = [];
105
+ let cur = node.parent;
106
+ while (cur) {
107
+ chain.push(cur);
108
+ cur = cur.parent;
109
+ }
110
+ let p = "";
111
+ let liIdx = 0;
112
+ for (let i = chain.length - 1; i >= 0; i--) {
113
+ const tagId = chain[i].tagId;
114
+ if (tagId === 22) p += "> ";
115
+ else if (tagId === 25) {
116
+ const w = state.listIndentWidths[liIdx] ?? 2;
117
+ p += " ".repeat(w);
118
+ liIdx++;
119
+ }
120
+ }
121
+ return p;
122
+ }
123
+ function wrapText(value, col, width, prefix) {
124
+ const leading = value.charCodeAt(0) === 32;
125
+ const trailing = value.charCodeAt(value.length - 1) === 32;
126
+ const prefixLen = [...prefix].length;
127
+ let out = "";
128
+ let first = true;
129
+ let i = 0;
130
+ const len = value.length;
131
+ while (i < len) {
132
+ let next = value.indexOf(" ", i);
133
+ if (next === -1) next = len;
134
+ if (next > i) {
135
+ const word = value.slice(i, next);
136
+ const wordLen = [...word].length;
137
+ const needSpace = first ? leading : true;
138
+ if (needSpace && col > prefixLen && col + 1 + wordLen > width) {
139
+ out += `\n${prefix}`;
140
+ col = prefixLen;
141
+ } else if (needSpace) {
142
+ out += " ";
143
+ col += 1;
144
+ }
145
+ out += word;
146
+ col += wordLen;
147
+ first = false;
148
+ }
149
+ i = next + 1;
150
+ }
151
+ if (trailing && out !== "" && !out.endsWith(" ") && !out.endsWith("\n")) out += " ";
152
+ if (out === "" && (leading || trailing)) out = " ";
153
+ return out;
154
+ }
88
155
  function calculateNewLineConfig(node) {
89
156
  const tagId = node.tagId;
90
157
  const depthMap = node.depthMap;
@@ -163,8 +230,15 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
163
230
  if (lastChar === "\n" && value[0] && value[0] !== "\n") value = indent + value;
164
231
  textNode.value = value;
165
232
  }
166
- state.buffer.push(textNode.value);
167
- state.lastContentCache = textNode.value;
233
+ const wrapWidth = state.options?.wrapWidth;
234
+ if (wrapWidth && canWrapHere(state.depthMap)) {
235
+ const wrapped = wrapText(textNode.value, currentColumn(state.buffer), wrapWidth, wrapContinuationPrefix(state, textNode));
236
+ state.buffer.push(wrapped);
237
+ state.lastContentCache = wrapped;
238
+ } else {
239
+ state.buffer.push(textNode.value);
240
+ state.lastContentCache = textNode.value;
241
+ }
168
242
  }
169
243
  state.lastTextNode = textNode;
170
244
  return;
@@ -137,6 +137,13 @@ interface EngineOptions {
137
137
  * final markdown (sync API only for `fragments`).
138
138
  */
139
139
  clean?: boolean | CleanOptions;
140
+ /**
141
+ * Hard-wrap prose at this many characters, breaking on word boundaries.
142
+ * Applied inline during conversion (zero-cost when unset). Code blocks
143
+ * (`<pre>`/`<code>`), tables, and headings are never wrapped. `0` disables
144
+ * wrapping.
145
+ */
146
+ wrapWidth?: number;
140
147
  }
141
148
  interface ElementNode extends Node {
142
149
  /** Element tag name (for ELEMENT_NODE) */
package/dist/cli.mjs CHANGED
@@ -6,7 +6,10 @@ import { fileURLToPath } from "node:url";
6
6
  import { cac } from "cac";
7
7
  import { dirname, join } from "pathe";
8
8
  async function streamingConvert(options = {}) {
9
- let conversionOptions = { origin: options.origin };
9
+ let conversionOptions = {
10
+ origin: options.origin,
11
+ wrapWidth: options.wrapWidth ? Number(options.wrapWidth) || void 0 : void 0
12
+ };
10
13
  if (options.preset === "minimal") conversionOptions = withMinimalPreset(conversionOptions);
11
14
  const markdownGenerator = streamHtmlToMarkdown(Readable.toWeb(process.stdin), conversionOptions);
12
15
  for await (const markdownChunk of markdownGenerator) if (markdownChunk && markdownChunk.length > 0) process.stdout.write(markdownChunk);
@@ -14,7 +17,7 @@ async function streamingConvert(options = {}) {
14
17
  const packageJsonPath = join(dirname(fileURLToPath(import.meta.url)), "..", "package.json");
15
18
  const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
16
19
  const cli = cac();
17
- cli.command("[options]", "Convert HTML from stdin to Markdown on stdout (JS engine)").option("--origin <url>", "Origin URL for resolving relative image paths").option("--preset <preset>", "Conversion presets: minimal").action(async (_, opts) => {
20
+ cli.command("[options]", "Convert HTML from stdin to Markdown on stdout (JS engine)").option("--origin <url>", "Origin URL for resolving relative image paths").option("--preset <preset>", "Conversion presets: minimal").option("--wrap-width <n>", "Hard-wrap prose at <n> characters on word boundaries").action(async (_, opts) => {
18
21
  await streamingConvert(opts);
19
22
  });
20
23
  cli.help().version(packageJson.version).parse();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "1.3.0",
4
+ "version": "1.4.0",
5
5
  "description": "JavaScript HTML-to-Markdown engine for mdream. Escape hatch for hooks and edge runtimes.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",