@headwall/markdown-to-blocks 1.4.0 → 1.5.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 CHANGED
@@ -7,6 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [1.5.0] - 2026-11-20
11
+
12
+ ### Added
13
+
14
+ - **Linked images.** A paragraph holding only a linked image
15
+ (`[![alt](src)](href)`) becomes a `core/image` with the link, instead of a
16
+ paragraph with an inline `<img>`. `linkDestination` is `"media"` when the
17
+ link is the image's own file (including WordPress's `-WxH` and `-scaled`
18
+ sizes), so lightboxes work, and `"custom"` otherwise. Link URLs follow the
19
+ `urlSchemes` policy: an unsafe link is dropped and the image renders
20
+ unlinked.
21
+ - **Captions from image titles.** `![alt](src "Caption")` renders a
22
+ `<figcaption>`. A `caption` from `resolveImage` still takes precedence.
23
+ Previously the title of an external image was silently dropped.
24
+
25
+ Both shapes are checked byte-for-byte against `@wordpress/blocks`' own
26
+ serialiser.
27
+
10
28
  ## [1.4.0] - 2026-11-20
11
29
 
12
30
  ### Security
@@ -152,7 +170,8 @@ parser and confirmed to import into the block editor with no recovery prompts.
152
170
  - ESM-only. Requires Node ≥ 18.
153
171
  - Zero runtime dependencies — all bundled into the published package.
154
172
 
155
- [Unreleased]: https://github.com/headwalluk/markdown-to-blocks/compare/v1.4.0...HEAD
173
+ [Unreleased]: https://github.com/headwalluk/markdown-to-blocks/compare/v1.5.0...HEAD
174
+ [1.5.0]: https://github.com/headwalluk/markdown-to-blocks/compare/v1.4.0...v1.5.0
156
175
  [1.4.0]: https://github.com/headwalluk/markdown-to-blocks/compare/v1.3.0...v1.4.0
157
176
  [1.3.0]: https://github.com/headwalluk/markdown-to-blocks/compare/v1.2.0...v1.3.0
158
177
  [1.2.0]: https://github.com/headwalluk/markdown-to-blocks/compare/v1.1.1...v1.2.0
package/README.md CHANGED
@@ -207,6 +207,25 @@ const html = markdownToBlocks(markdown, {
207
207
  - An `id` must be a positive integer; anything else throws a `RangeError`.
208
208
  - The final URL, whether from the Markdown or the resolver, is subject to the [URL safety](#url-safety) policy.
209
209
 
210
+ ### Captions and links
211
+
212
+ An image's Markdown title becomes its caption, unless the resolver supplies a `caption`:
213
+
214
+ ```markdown
215
+ ![Floor plan](https://example.com/wp-content/uploads/plan.png "Ground floor, as built")
216
+ ```
217
+
218
+ An image wrapped in a link, alone in its paragraph, becomes a linked `core/image`:
219
+
220
+ ```markdown
221
+ [![Floor plan](https://example.com/wp-content/uploads/plan-1024x768.png)](https://example.com/wp-content/uploads/plan.png)
222
+ ```
223
+
224
+ - When the link points at the image's own file, `linkDestination` is `"media"`: clicking opens the full-size image, which is what lightbox plugins and themes look for. Any size WordPress generated counts as the same file (`-1024x768`, `-scaled`), as does a relative URL with the same path.
225
+ - Any other link is `"custom"`.
226
+ - A link whose URL fails the [URL safety](#url-safety) policy is dropped, and the image renders unlinked.
227
+ - A linked image inside a sentence stays inline in its paragraph, as before.
228
+
210
229
  ---
211
230
 
212
231
  ## Untrusted Markdown
@@ -250,7 +269,7 @@ By default `headingShift` is `0` and `minHeadingLevel` is `2`, so a body `#` bec
250
269
  | Blockquotes | `core/quote` (with inner paragraph blocks) |
251
270
  | Thematic breaks `---` `***` `___` | `core/separator` |
252
271
  | Tables (GFM) | `core/table` |
253
- | Images | `core/image` (resolver-driven) |
272
+ | Images, linked images `[![alt](src)](href)` | `core/image` (resolver-driven; title → caption) |
254
273
  | Raw HTML in source | `core/html` |
255
274
  | **bold**, *italic*, `code`, ~~strike~~, links, hard breaks | inline HTML |
256
275
  | GFM task lists | leading `☐` / `☑` glyph (not interactive inputs) |
@@ -19908,37 +19908,69 @@ function serializeBlockAttributes(attributes) {
19908
19908
  }
19909
19909
 
19910
19910
  // src/blocks/image.ts
19911
- function renderImage(node2, ctx) {
19911
+ function renderImage(node2, ctx, link2) {
19912
19912
  const image2 = resolveMarkdownImage(node2, ctx.options);
19913
19913
  let out = null;
19914
19914
  if (image2 != null) {
19915
- out = image2.id != null ? renderLibraryImage(image2, image2.id) : renderExternalImage(image2);
19915
+ const caption = image2.caption ?? node2.title ?? void 0;
19916
+ const imageLink = link2 != null ? resolveImageLink(link2, image2.url, ctx) : null;
19917
+ out = image2.id != null ? renderLibraryImage(image2, image2.id, caption, imageLink) : renderExternalImage(image2, caption, imageLink);
19916
19918
  }
19917
19919
  return out;
19918
19920
  }
19919
- function renderLibraryImage(image2, id) {
19921
+ function renderLibraryImage(image2, id, caption, link2) {
19920
19922
  const sizeSlug = image2.sizeSlug;
19921
19923
  const attrs = { id };
19922
19924
  if (sizeSlug != null) {
19923
19925
  attrs.sizeSlug = sizeSlug;
19924
19926
  }
19925
- attrs.linkDestination = "none";
19927
+ attrs.linkDestination = link2?.destination ?? "none";
19926
19928
  const figureClass = sizeSlug != null ? `wp-block-image size-${escapeAttribute(sizeSlug)}` : "wp-block-image";
19927
19929
  const img = `<img src="${escapeAttribute(image2.url)}" alt="${escapeAttribute(image2.alt ?? "")}" class="wp-image-${id}"/>`;
19928
- const caption = renderCaption(image2.caption);
19929
- const inner = `<figure class="${figureClass}">${img}${caption}</figure>`;
19930
+ const inner = `<figure class="${figureClass}">${wrapInLink(img, link2)}${renderCaption(caption)}</figure>`;
19930
19931
  return `<!-- wp:image${serializeBlockAttributes(attrs)} -->
19931
19932
  ${inner}
19932
19933
  <!-- /wp:image -->`;
19933
19934
  }
19934
- function renderExternalImage(image2) {
19935
+ function renderExternalImage(image2, caption, link2) {
19935
19936
  const img = `<img src="${escapeAttribute(image2.url)}" alt="${escapeAttribute(image2.alt ?? "")}"/>`;
19936
- const caption = renderCaption(image2.caption);
19937
- const inner = `<figure class="wp-block-image">${img}${caption}</figure>`;
19938
- return `<!-- wp:image -->
19937
+ const attrs = {};
19938
+ if (link2 != null) {
19939
+ attrs.linkDestination = link2.destination;
19940
+ }
19941
+ const inner = `<figure class="wp-block-image">${wrapInLink(img, link2)}${renderCaption(caption)}</figure>`;
19942
+ return `<!-- wp:image${serializeBlockAttributes(attrs)} -->
19939
19943
  ${inner}
19940
19944
  <!-- /wp:image -->`;
19941
19945
  }
19946
+ function resolveImageLink(link2, imageUrl, ctx) {
19947
+ const allowed = ctx.options.urlSchemes === "any" || isSafeLinkUrl(link2.url);
19948
+ let imageLink = null;
19949
+ if (allowed) {
19950
+ imageLink = {
19951
+ href: link2.url,
19952
+ destination: isSameMediaFile(link2.url, imageUrl) ? "media" : "custom"
19953
+ };
19954
+ }
19955
+ return imageLink;
19956
+ }
19957
+ function wrapInLink(img, link2) {
19958
+ return link2 != null ? `<a href="${escapeAttribute(link2.href)}">${img}</a>` : img;
19959
+ }
19960
+ function isSameMediaFile(href, src) {
19961
+ const hrefFile = mediaFileKey(href);
19962
+ const srcFile = mediaFileKey(src);
19963
+ const hostsMatch = hrefFile.host == null || srcFile.host == null || hrefFile.host === srcFile.host;
19964
+ return hostsMatch && hrefFile.path !== "" && hrefFile.path === srcFile.path;
19965
+ }
19966
+ function mediaFileKey(url) {
19967
+ const withoutSuffix = url.replace(/[?#].*$/, "");
19968
+ const match = /^[a-z][a-z0-9+.-]*:\/\/([^/]*)(.*)$/i.exec(withoutSuffix);
19969
+ const host = match?.[1] != null ? match[1].toLowerCase() : null;
19970
+ const path2 = match != null ? match[2] ?? "" : withoutSuffix;
19971
+ const basePath = path2.replace(/-\d+x\d+(\.[a-z0-9]+)$/i, "$1").replace(/-scaled(\.[a-z0-9]+)$/i, "$1");
19972
+ return { host, path: basePath };
19973
+ }
19942
19974
  function renderCaption(caption) {
19943
19975
  if (caption == null || caption === "") {
19944
19976
  return "";
@@ -20030,8 +20062,12 @@ ${li}
20030
20062
 
20031
20063
  // src/blocks/paragraph.ts
20032
20064
  function renderParagraph(node2, ctx) {
20033
- if (node2.children.length === 1 && node2.children[0]?.type === "image") {
20034
- return renderImage(node2.children[0], ctx);
20065
+ const only = node2.children.length === 1 ? node2.children[0] : void 0;
20066
+ if (only?.type === "image") {
20067
+ return renderImage(only, ctx);
20068
+ }
20069
+ if (only?.type === "link" && only.children.length === 1 && only.children[0]?.type === "image") {
20070
+ return renderImage(only.children[0], ctx, only);
20035
20071
  }
20036
20072
  const inner = ctx.renderInline(node2.children);
20037
20073
  if (inner.trim() === "") {
@@ -20268,4 +20304,4 @@ export {
20268
20304
  markdownToBlocks,
20269
20305
  parseMarkdownDocument
20270
20306
  };
20271
- //# sourceMappingURL=chunk-26IEHNWU.js.map
20307
+ //# sourceMappingURL=chunk-VRPK6PTA.js.map