@headwall/markdown-to-blocks 1.1.0 → 1.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.
- package/CHANGELOG.md +29 -1
- package/README.md +3 -2
- package/dist/{chunk-73P6MFHU.js → chunk-CHMDS2SX.js} +96 -6
- package/dist/chunk-CHMDS2SX.js.map +1 -0
- package/dist/cli.js +14 -2
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.js +1 -1
- package/package.json +5 -2
- package/dist/chunk-73P6MFHU.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,32 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.2.0] - 2026-07-05
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- `headingAnchors` option (CLI `--heading-anchors`): derive an `id` (WordPress
|
|
15
|
+
`anchor` attribute) for each heading from its text, so in-page anchor links
|
|
16
|
+
work. GitHub-style slugs, de-duplicated per document (`overview`,
|
|
17
|
+
`overview-1`, …); a heading that slugs to nothing (only punctuation/emoji)
|
|
18
|
+
gets no `id`. Off by default. Output matches core's serialiser byte-for-byte
|
|
19
|
+
(including `--` attribute escaping) and validates against the
|
|
20
|
+
WordPress parser.
|
|
21
|
+
|
|
22
|
+
## [1.1.1] - 2026-06-29
|
|
23
|
+
|
|
24
|
+
### Fixed
|
|
25
|
+
|
|
26
|
+
- Reference-style links (`[text][ref]`) and images (`![alt][ref]`) are now
|
|
27
|
+
resolved against their `[ref]: url` definitions instead of being silently
|
|
28
|
+
dropped (links lost their URL; images vanished entirely).
|
|
29
|
+
|
|
30
|
+
### Changed
|
|
31
|
+
|
|
32
|
+
- CLI: exit with a clear "no input" message and usage hint instead of hanging
|
|
33
|
+
when run on an interactive terminal with no file argument and nothing piped to
|
|
34
|
+
stdin.
|
|
35
|
+
|
|
10
36
|
## [1.1.0] - 2026-06-28
|
|
11
37
|
|
|
12
38
|
### Changed
|
|
@@ -52,6 +78,8 @@ parser and confirmed to import into the block editor with no recovery prompts.
|
|
|
52
78
|
- ESM-only. Requires Node ≥ 18.
|
|
53
79
|
- Zero runtime dependencies — all bundled into the published package.
|
|
54
80
|
|
|
55
|
-
[Unreleased]: https://github.com/headwalluk/markdown-to-blocks/compare/v1.
|
|
81
|
+
[Unreleased]: https://github.com/headwalluk/markdown-to-blocks/compare/v1.2.0...HEAD
|
|
82
|
+
[1.2.0]: https://github.com/headwalluk/markdown-to-blocks/compare/v1.1.1...v1.2.0
|
|
83
|
+
[1.1.1]: https://github.com/headwalluk/markdown-to-blocks/compare/v1.1.0...v1.1.1
|
|
56
84
|
[1.1.0]: https://github.com/headwalluk/markdown-to-blocks/compare/v1.0.0...v1.1.0
|
|
57
85
|
[1.0.0]: https://github.com/headwalluk/markdown-to-blocks/releases/tag/v1.0.0
|
package/README.md
CHANGED
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
> Convert Markdown into serialised WordPress/Gutenberg **block markup** — HTML with `<!-- wp:… -->` delimiters that drops straight into the block editor with no "Convert to blocks" or "this block contains unexpected content" prompts.
|
|
4
4
|
|
|
5
5
|
[](https://github.com/headwalluk/markdown-to-blocks/actions/workflows/ci.yml)
|
|
6
|
-
[](https://www.npmjs.com/package/@headwall/markdown-to-blocks)
|
|
6
|
+
[](https://www.npmjs.com/package/@headwall/markdown-to-blocks)
|
|
8
7
|
[](./LICENSE)
|
|
9
8
|
[](https://nodejs.org)
|
|
10
9
|
[](https://www.typescriptlang.org/)
|
|
@@ -103,6 +102,7 @@ Modes:
|
|
|
103
102
|
Conversion options:
|
|
104
103
|
--heading-shift <n> Shift all heading levels by n (default 0)
|
|
105
104
|
--min-heading-level <n> Clamp heading levels to [n, 6] (default 2)
|
|
105
|
+
--heading-anchors Add an id/anchor to each heading from its text
|
|
106
106
|
--code-language <mode> 'drop' (default) or 'class' (emit language-xxx)
|
|
107
107
|
--unsupported <mode> 'html' (default, wrap in core/html) or 'skip'
|
|
108
108
|
--no-gfm Disable GFM (tables, strikethrough, autolinks)
|
|
@@ -162,6 +162,7 @@ Body text.
|
|
|
162
162
|
|---|---|---|---|
|
|
163
163
|
| `headingShift` | `number` | `0` | Shift every heading level by this amount before clamping. |
|
|
164
164
|
| `minHeadingLevel` | `number` | `2` | Clamp resulting heading levels into `[min, 6]`. The default of `2` keeps body content from emitting an `h1`. |
|
|
165
|
+
| `headingAnchors` | `boolean` | `false` | Derive an `id` (WordPress `anchor` attribute) for each heading from its text, so in-page anchor links work. GitHub-style slugs, de-duplicated per document (`overview`, `overview-1`, …). A heading that slugs to nothing (only punctuation/emoji) gets no `id`. |
|
|
165
166
|
| `resolveImage` | `(src, alt, title?) => ResolvedImage \| undefined` | — | Resolve a Markdown image to a WordPress image (see [Images](#images)). |
|
|
166
167
|
| `codeLanguage` | `'drop' \| 'class'` | `'drop'` | `'drop'` discards the fence language (core behaviour). `'class'` emits `<code class="language-xxx">` for a syntax-highlighting plugin. |
|
|
167
168
|
| `unsupported` | `'html' \| 'skip'` | `'html'` | How to handle raw/unsupported nodes: wrap in `core/html`, or omit. |
|
|
@@ -13,7 +13,11 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
13
13
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
14
14
|
});
|
|
15
15
|
var __commonJS = (cb, mod) => function __require2() {
|
|
16
|
-
|
|
16
|
+
try {
|
|
17
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
18
|
+
} catch (e) {
|
|
19
|
+
throw mod = 0, e;
|
|
20
|
+
}
|
|
17
21
|
};
|
|
18
22
|
var __export = (target, all2) => {
|
|
19
23
|
for (var name in all2)
|
|
@@ -19556,11 +19560,36 @@ function renderUnknownInline(node2, options) {
|
|
|
19556
19560
|
return out;
|
|
19557
19561
|
}
|
|
19558
19562
|
|
|
19563
|
+
// src/slug.ts
|
|
19564
|
+
function slugify(text5) {
|
|
19565
|
+
const slug = text5.toLowerCase().replace(/[^\p{L}\p{N}_ -]+/gu, "").replace(/ /g, "-");
|
|
19566
|
+
return slug;
|
|
19567
|
+
}
|
|
19568
|
+
function createHeadingSlugger() {
|
|
19569
|
+
const occurrences = /* @__PURE__ */ new Map();
|
|
19570
|
+
return {
|
|
19571
|
+
slug(text5) {
|
|
19572
|
+
const base = slugify(text5);
|
|
19573
|
+
let result = base;
|
|
19574
|
+
if (base !== "") {
|
|
19575
|
+
while (occurrences.has(result)) {
|
|
19576
|
+
const next = (occurrences.get(base) ?? 0) + 1;
|
|
19577
|
+
occurrences.set(base, next);
|
|
19578
|
+
result = `${base}-${next}`;
|
|
19579
|
+
}
|
|
19580
|
+
occurrences.set(result, 0);
|
|
19581
|
+
}
|
|
19582
|
+
return result;
|
|
19583
|
+
}
|
|
19584
|
+
};
|
|
19585
|
+
}
|
|
19586
|
+
|
|
19559
19587
|
// src/types.ts
|
|
19560
19588
|
function resolveOptions(options) {
|
|
19561
19589
|
return {
|
|
19562
19590
|
headingShift: options?.headingShift ?? 0,
|
|
19563
19591
|
minHeadingLevel: options?.minHeadingLevel ?? 2,
|
|
19592
|
+
headingAnchors: options?.headingAnchors ?? false,
|
|
19564
19593
|
resolveImage: options?.resolveImage,
|
|
19565
19594
|
codeLanguage: options?.codeLanguage ?? "drop",
|
|
19566
19595
|
unsupported: options?.unsupported ?? "html",
|
|
@@ -19635,20 +19664,51 @@ function renderCaption(caption) {
|
|
|
19635
19664
|
return `<figcaption class="wp-element-caption">${escapeText(caption)}</figcaption>`;
|
|
19636
19665
|
}
|
|
19637
19666
|
|
|
19667
|
+
// src/attributes.ts
|
|
19668
|
+
function serializeBlockAttributes(attributes) {
|
|
19669
|
+
let out = "";
|
|
19670
|
+
if (Object.keys(attributes).length > 0) {
|
|
19671
|
+
const json = JSON.stringify(attributes).replace(/--/g, "\\u002d\\u002d").replace(/</g, "\\u003c").replace(/>/g, "\\u003e").replace(/&/g, "\\u0026").replace(/ /g, "\\u00a0");
|
|
19672
|
+
out = ` ${json}`;
|
|
19673
|
+
}
|
|
19674
|
+
return out;
|
|
19675
|
+
}
|
|
19676
|
+
|
|
19638
19677
|
// src/blocks/heading.ts
|
|
19639
19678
|
function renderHeading(node2, ctx) {
|
|
19640
|
-
const { headingShift, minHeadingLevel } = ctx.options;
|
|
19679
|
+
const { headingShift, minHeadingLevel, headingAnchors } = ctx.options;
|
|
19641
19680
|
const shifted = node2.depth + headingShift;
|
|
19642
19681
|
const level = Math.min(6, Math.max(minHeadingLevel, shifted));
|
|
19643
19682
|
const inner = ctx.renderInline(node2.children);
|
|
19644
|
-
const
|
|
19645
|
-
const
|
|
19646
|
-
|
|
19683
|
+
const anchor = headingAnchors ? ctx.slugger.slug(headingText(node2.children)) : "";
|
|
19684
|
+
const attributes = {};
|
|
19685
|
+
if (level !== 2) {
|
|
19686
|
+
attributes.level = level;
|
|
19687
|
+
}
|
|
19688
|
+
if (anchor !== "") {
|
|
19689
|
+
attributes.anchor = anchor;
|
|
19690
|
+
}
|
|
19691
|
+
const idAttr = anchor !== "" ? ` id="${anchor}"` : "";
|
|
19692
|
+
const open = `<!-- wp:heading${serializeBlockAttributes(attributes)} -->`;
|
|
19693
|
+
const tag = `<h${level}${idAttr} class="wp-block-heading">${inner}</h${level}>`;
|
|
19647
19694
|
const close2 = "<!-- /wp:heading -->";
|
|
19648
19695
|
return `${open}
|
|
19649
19696
|
${tag}
|
|
19650
19697
|
${close2}`;
|
|
19651
19698
|
}
|
|
19699
|
+
function headingText(nodes) {
|
|
19700
|
+
let text5 = "";
|
|
19701
|
+
for (const node2 of nodes) {
|
|
19702
|
+
const value2 = node2.value;
|
|
19703
|
+
const children = node2.children;
|
|
19704
|
+
if (typeof value2 === "string") {
|
|
19705
|
+
text5 += value2;
|
|
19706
|
+
} else if (children != null) {
|
|
19707
|
+
text5 += headingText(children);
|
|
19708
|
+
}
|
|
19709
|
+
}
|
|
19710
|
+
return text5;
|
|
19711
|
+
}
|
|
19652
19712
|
|
|
19653
19713
|
// src/blocks/list.ts
|
|
19654
19714
|
function renderList(node2, ctx) {
|
|
@@ -19751,11 +19811,41 @@ function parseMarkdown(markdown, options) {
|
|
|
19751
19811
|
const processor = options.gfm ? base.use(remarkGfm) : base;
|
|
19752
19812
|
const tree = processor.parse(markdown);
|
|
19753
19813
|
const root2 = processor.runSync(tree);
|
|
19814
|
+
resolveReferences(root2);
|
|
19754
19815
|
return root2;
|
|
19755
19816
|
}
|
|
19817
|
+
function resolveReferences(tree) {
|
|
19818
|
+
const definitions = /* @__PURE__ */ new Map();
|
|
19819
|
+
visit(tree, "definition", (node2) => {
|
|
19820
|
+
definitions.set(node2.identifier, node2);
|
|
19821
|
+
});
|
|
19822
|
+
visit(tree, "linkReference", (node2) => {
|
|
19823
|
+
const def = definitions.get(node2.identifier);
|
|
19824
|
+
if (def == null) {
|
|
19825
|
+
return;
|
|
19826
|
+
}
|
|
19827
|
+
const link2 = node2;
|
|
19828
|
+
link2.type = "link";
|
|
19829
|
+
link2.url = def.url;
|
|
19830
|
+
link2.title = def.title ?? null;
|
|
19831
|
+
});
|
|
19832
|
+
visit(tree, "imageReference", (node2) => {
|
|
19833
|
+
const def = definitions.get(node2.identifier);
|
|
19834
|
+
if (def == null) {
|
|
19835
|
+
return;
|
|
19836
|
+
}
|
|
19837
|
+
const alt = node2.alt ?? "";
|
|
19838
|
+
const image2 = node2;
|
|
19839
|
+
image2.type = "image";
|
|
19840
|
+
image2.url = def.url;
|
|
19841
|
+
image2.title = def.title ?? null;
|
|
19842
|
+
image2.alt = alt;
|
|
19843
|
+
});
|
|
19844
|
+
}
|
|
19756
19845
|
function createContext(options) {
|
|
19757
19846
|
const context = {
|
|
19758
19847
|
options,
|
|
19848
|
+
slugger: createHeadingSlugger(),
|
|
19759
19849
|
renderInline(nodes) {
|
|
19760
19850
|
return renderInline(nodes, options);
|
|
19761
19851
|
},
|
|
@@ -19876,4 +19966,4 @@ export {
|
|
|
19876
19966
|
markdownToBlocks,
|
|
19877
19967
|
parseMarkdownDocument
|
|
19878
19968
|
};
|
|
19879
|
-
//# sourceMappingURL=chunk-
|
|
19969
|
+
//# sourceMappingURL=chunk-CHMDS2SX.js.map
|