@md2nativedocx/pandoc-filter 0.1.0 → 0.2.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.
@@ -10,6 +10,12 @@
10
10
  * shell string.
11
11
  *
12
12
  * Usage: md2nativedocx-core.mjs <diagram.mmd> > diagram.xml
13
+ * md2nativedocx-core.mjs <diagram.mmd> --odf <idPrefix> > diagram.json
14
+ *
15
+ * With `--odf`, the diagram is rendered for a `.odt` by `renderDiagramOdf()` (ADR 0013) and stdout is
16
+ * one JSON object `{ "fragment": "<text:p>…", "automaticStyles": ["<style:style …>", …] }`: the Lua
17
+ * filter inserts the fragment as a raw `opendocument` block and passes the styles to the derived
18
+ * template. `idPrefix` (from the filter's own counter) keeps ids and style names unique per document.
13
19
  *
14
20
  * ## Adapter only (ADR 0012)
15
21
  *
@@ -49,9 +55,16 @@
49
55
  import { readFileSync, mkdirSync, writeFileSync } from 'node:fs';
50
56
  import { randomUUID } from 'node:crypto';
51
57
  import { join } from 'node:path';
52
- import { renderDiagram } from '@md2nativedocx/core';
58
+ import {
59
+ renderDiagram,
60
+ renderDiagramOdf,
61
+ buildDiagramTooLargeNoteXml,
62
+ buildOdfDiagramTooLargeNote,
63
+ DiagramTooLargeError,
64
+ } from '@md2nativedocx/core';
53
65
 
54
66
  const inputPath = process.argv[2];
67
+ const odfPrefix = process.argv[3] === '--odf' ? process.argv[4] : undefined;
55
68
  const input = inputPath ? readFileSync(inputPath, 'utf8') : readFileSync(0, 'utf8');
56
69
 
57
70
  /** A positive integer from the environment, or `undefined`. */
@@ -103,13 +116,41 @@ function writePart(part) {
103
116
  }
104
117
  }
105
118
 
106
- try {
107
- const result = renderDiagram(input, options);
108
- for (const warning of result.metadata.warnings) {
109
- process.stderr.write(`md2nativedocx: warning: ${warning}\n`);
119
+ /** `.odt` mode: same limits and page size, no SmartArt or charts (no ODF equivalent yet). */
120
+ function renderOdf() {
121
+ const odfOptions = { idPrefix: odfPrefix };
122
+ for (const key of ['maxDrawingCx', 'maxDrawingCy']) if (options[key] !== undefined) odfOptions[key] = options[key];
123
+ try {
124
+ const result = renderDiagramOdf(input, odfOptions);
125
+ for (const warning of result.metadata.warnings) process.stderr.write(`md2nativedocx: warning: ${warning}\n`);
126
+ return { fragment: result.fragment, automaticStyles: result.automaticStyles };
127
+ } catch (err) {
128
+ if (!(err instanceof DiagramTooLargeError)) throw err;
129
+ // Deliberate fallback, as for .docx: a visible note, the rest of the document converts.
130
+ process.stderr.write(`md2nativedocx: warning: ${err.message}\n`);
131
+ return buildOdfDiagramTooLargeNote(err, odfPrefix);
132
+ }
133
+ }
134
+
135
+ /** `.docx` mode: the WordprocessingML fragment, its SmartArt/chart parts written for the CLI. */
136
+ function renderDocx() {
137
+ try {
138
+ const result = renderDiagram(input, options);
139
+ for (const warning of result.metadata.warnings) {
140
+ process.stderr.write(`md2nativedocx: warning: ${warning}\n`);
141
+ }
142
+ for (const part of result.parts) writePart(part);
143
+ return result.fragment;
144
+ } catch (err) {
145
+ if (!(err instanceof DiagramTooLargeError)) throw err;
146
+ // Deliberate fallback: an oversized diagram becomes a visible note, the rest of the document converts.
147
+ process.stderr.write(`md2nativedocx: warning: ${err.message}\n`);
148
+ return buildDiagramTooLargeNoteXml(err);
110
149
  }
111
- for (const part of result.parts) writePart(part);
112
- process.stdout.write(result.fragment);
150
+ }
151
+
152
+ try {
153
+ process.stdout.write(odfPrefix !== undefined ? JSON.stringify(renderOdf()) : renderDocx());
113
154
  } catch (err) {
114
155
  const message = err instanceof Error ? err.message : String(err);
115
156
  process.stderr.write(`md2nativedocx: ${message}\n`);
package/md2nativedocx.lua CHANGED
@@ -4,6 +4,13 @@
4
4
  -- (wpg:wgp) via the md2nativedocx core engine, emitted as a
5
5
  -- pandoc.RawBlock('openxml', ...) (ADR 0002).
6
6
  --
7
+ -- For an ODT output (`-o doc.odt`, ADR 0013) each block becomes a
8
+ -- pandoc.RawBlock('opendocument', ...) instead, and the automatic styles it
9
+ -- needs are collected into the `md2n-automatic-styles` metadata list, which
10
+ -- the derived template (packages/cli/assets/md2nativedocx.opendocument)
11
+ -- writes inside office:automatic-styles. Nothing touches the .odt afterwards
12
+ -- (AGENTS.md rule 7).
13
+ --
7
14
  -- Security (AGENTS.md):
8
15
  -- * Rule #4: the core binary is invoked with a FIXED argument array and the
9
16
  -- diagram text is piped via stdin — never interpolated into a shell string.
@@ -34,17 +41,21 @@ local is_windows = package.config:sub(1, 1) == '\\'
34
41
  -- Windows) strips the outermost pair when the string starts with a quote —
35
42
  -- needed here since `node_bin` itself can contain spaces (e.g. "...\Microsoft
36
43
  -- VS Code\Code.exe" when it's the editor's own binary).
37
- local function core_command(tmp)
44
+ --
45
+ -- `odf_prefix`, when set, is a prefix this filter generated itself
46
+ -- ('md2n' .. a counter), never document text.
47
+ local function core_command(tmp, odf_prefix)
48
+ local odf_args = odf_prefix and (' --odf ' .. odf_prefix) or ''
38
49
  if not is_windows then
39
- return core_bin .. ' ' .. tmp
50
+ return core_bin .. ' ' .. tmp .. odf_args
40
51
  end
41
52
  local node_bin = os.getenv('MD2NATIVEDOCX_NODE_BIN') or 'node'
42
- return '""' .. node_bin .. '" "' .. core_bin .. '" "' .. tmp .. '""'
53
+ return '""' .. node_bin .. '" "' .. core_bin .. '" "' .. tmp .. '"' .. odf_args .. '"'
43
54
  end
44
55
 
45
56
  -- File-based bridge: write the diagram to a temp file, invoke the core with a
46
57
  -- fixed argument array (no shell interpolation of the diagram), read the XML.
47
- local function run_core_file(mermaid_text)
58
+ local function run_core_file(mermaid_text, odf_prefix)
48
59
  local xml, err
49
60
  -- Deliberately not os.tmpname(): a real 2026-09-08 incident found it
50
61
  -- crashing pandoc 3.9.0.2 on Windows with an access violation inside its
@@ -67,7 +78,7 @@ local function run_core_file(mermaid_text)
67
78
  -- only thing interpolated into the shell string is the trusted binary
68
79
  -- path and the temp file path (which we control), never the diagram
69
80
  -- text.
70
- local p = io.popen(core_command(tmp), 'r')
81
+ local p = io.popen(core_command(tmp, odf_prefix), 'r')
71
82
  if not p then
72
83
  err = 'md2nativedocx: could not start core bridge'
73
84
  return
@@ -93,16 +104,48 @@ end
93
104
  -- Filter 1: ```mermaid code blocks -> native OOXML/DrawingML (unchanged
94
105
  -- behaviour from before this file returned an explicit filter list — see
95
106
  -- the doc comment on the returned table below for why it now has to be one).
107
+ local is_odt = FORMAT == 'odt' or FORMAT == 'opendocument'
108
+ local odf_count = 0
109
+ local odf_styles = pandoc.List()
110
+
96
111
  local mermaid_filter = {
97
112
  CodeBlock = function(el)
98
- if el.classes[1] == 'mermaid' then
99
- local xml, err = run_core_file(el.text)
100
- if not xml then
113
+ if el.classes[1] ~= 'mermaid' then return nil end
114
+ if is_odt then
115
+ odf_count = odf_count + 1
116
+ local out, err = run_core_file(el.text, 'md2n' .. odf_count)
117
+ if not out then
101
118
  io.stderr:write(err .. '\n')
102
119
  return nil
103
120
  end
104
- return pandoc.RawBlock('openxml', xml)
121
+ local ok, result = pcall(pandoc.json.decode, out, false)
122
+ if not ok or type(result) ~= 'table' or type(result.fragment) ~= 'string' then
123
+ io.stderr:write('md2nativedocx: core bridge returned unreadable ODF output\n')
124
+ return nil
125
+ end
126
+ for _, style in ipairs(result.automaticStyles or {}) do
127
+ odf_styles:insert(pandoc.MetaInlines({ pandoc.RawInline('opendocument', style) }))
128
+ end
129
+ return pandoc.RawBlock('opendocument', result.fragment)
130
+ end
131
+ local xml, err = run_core_file(el.text)
132
+ if not xml then
133
+ io.stderr:write(err .. '\n')
134
+ return nil
105
135
  end
136
+ return pandoc.RawBlock('openxml', xml)
137
+ end,
138
+ }
139
+
140
+ -- Filter 1b: hand the diagram styles collected above to the template. A
141
+ -- separate table, run after filter 1, for the reason given at the end of
142
+ -- this file.
143
+ local odf_styles_filter = {
144
+ Pandoc = function(doc)
145
+ if is_odt and #odf_styles > 0 then
146
+ doc.meta['md2n-automatic-styles'] = pandoc.MetaList(odf_styles)
147
+ end
148
+ return doc
106
149
  end,
107
150
  }
108
151
 
@@ -133,7 +176,7 @@ local mermaid_filter = {
133
176
  -- by Table) — a Header separated from its Table by some other block (e.g. an
134
177
  -- intervening paragraph) is deliberately not matched, same scope limitation
135
178
  -- flagged in ADR 0005 ("non couvert par ce spike").
136
- local landscape_enabled = os.getenv('MD2NATIVEDOCX_LANDSCAPE_TABLES') == '1'
179
+ local landscape_enabled = os.getenv('MD2NATIVEDOCX_LANDSCAPE_TABLES') == '1' and FORMAT == 'docx'
137
180
 
138
181
  local function env_twips(name, fallback)
139
182
  local raw = os.getenv(name)
@@ -197,4 +240,4 @@ local landscape_table_filter = {
197
240
  -- present in the SAME filter table as element handlers like `CodeBlock`
198
241
  -- would make Pandoc ignore those element handlers entirely — two separate
199
242
  -- tables keep the mermaid conversion above completely unaffected.
200
- return { mermaid_filter, landscape_table_filter }
243
+ return { mermaid_filter, odf_styles_filter, landscape_table_filter }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@md2nativedocx/pandoc-filter",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Pandoc Lua filter for md2nativedocx (Markdown to native Word): converts ```mermaid code blocks in a Markdown-to-docx conversion into editable OOXML shapes.",
5
5
  "license": "CC0-1.0",
6
6
  "repository": {
@@ -21,6 +21,6 @@
21
21
  "test:ci": "node --test --test-reporter=spec --test-reporter-destination=stdout --test-reporter=junit --test-reporter-destination=reports/junit.xml test/*.test.mjs"
22
22
  },
23
23
  "dependencies": {
24
- "@md2nativedocx/core": "0.1.0"
24
+ "@md2nativedocx/core": "0.2.0"
25
25
  }
26
26
  }