Changes
3 changed files (+90/-25)
-
-
@@ -6,6 +6,10 @@ import { assertObjectMatch } from "../../deps/deno.land/std/assert/mod.ts";import { fromMarkdown } from "../../deps/esm.sh/mdast-util-from-markdown/mod.ts"; import { toHast } from "../../deps/esm.sh/mdast-util-to-hast/mod.ts"; import { ofmWikilink } from "./micromark_extension_ofm_wikilink.ts"; import { ofmWikilinkFromMarkdown } from "./mdast_util_ofm_wikilink.ts"; import { ofmImageSize } from "./mdast_util_ofm_image_size.ts"; Deno.test("Should parse full size attribute", () => {
-
@@ -25,6 +29,33 @@ alt: "Foo",data: { width: 100, height: 200, }, }, ], }, ], }); }); Deno.test("Should parse for wikilink embeds", () => { const mdast = fromMarkdown("![[Foo|999x9]]", { extensions: [ofmWikilink()], mdastExtensions: [ofmWikilinkFromMarkdown()], }); ofmImageSize(mdast); assertObjectMatch(mdast, { type: "root", children: [ { type: "paragraph", children: [ { type: "ofmWikilinkEmbed", data: { width: 999, height: 9, }, }, ],
-
-
-
@@ -5,6 +5,8 @@import type * as Mdast from "../../deps/esm.sh/mdast/types.ts"; import { SKIP, visit } from "../../deps/esm.sh/unist-util-visit/mod.ts"; import type { OfmWikilinkEmbed } from "./mdast_util_ofm_wikilink.ts"; const SEPARATOR = "|"; const SIZE_ATTR_REGEXP_PATTERN = /^([1-9][0-9]*)(x([1-9][0-9]*))?$/;
-
@@ -63,43 +65,59 @@ * image label and data in-place.* * @param tree - Tree to change. This function mutates this argument. */ export function ofmImageSize(tree: Mdast.Nodes): void { export function ofmImageSize(tree: Mdast.Nodes | OfmWikilinkEmbed): void { visit(tree, (node) => { return node.type === "image" || node.type === "imageReference"; }, (node_) => { // Tested on the `check` function. const node = node_ as (Mdast.Image | Mdast.ImageReference); return node.type === "image" || node.type === "imageReference" || node.type === "ofmWikilinkEmbed"; }, (node) => { switch (node.type) { case "image": case "imageReference": { if (!node.alt) { return SKIP; } if (!node.alt) { return SKIP; } const segments = node.alt.split(SEPARATOR); const segments = node.alt.split(SEPARATOR); switch (segments.length) { case 1: { const result = parseSegment(segments[0]); if (!result) { return SKIP; } switch (segments.length) { case 1: { const result = parseSegment(segments[0]); if (!result) { setSizeToNode(node, result.width, result.height); return; } case 2: { const [alt, segment] = segments; const result = parseSegment(segment); if (!result) { return SKIP; } setSizeToNode(node, result.width, result.height); node.alt = alt; return; } default: { return SKIP; } } } case "ofmWikilinkEmbed": { if (!node.label) { return SKIP; } setSizeToNode(node, result.width, result.height); return; } case 2: { const [alt, segment] = segments; const result = parseSegment(segment); const result = parseSegment(node.label); if (!result) { return SKIP; } setSizeToNode(node, result.width, result.height); node.alt = alt; return; } default: { return SKIP; } }
-
-
-
@@ -88,6 +88,19 @@ },}; } function sizeProperties(node: Mdast.Node): { width?: number; height?: number } { return { width: node.data && "width" in node.data && typeof node.data.width === "number" ? node.data.width : undefined, height: node.data && "height" in node.data && typeof node.data.height === "number" ? node.data.height : undefined, }; } export const ofmWikilinkToHastHandlers = { ofmWikilink(_state: State, node: OfmWikilink): Hast.Nodes { return {
-
@@ -112,6 +125,7 @@ return {type: "element", tagName: "img", properties: { ...sizeProperties(node), src: node.target, alt: node.label ?? undefined, },
-
@@ -143,6 +157,7 @@ return {type: "element", tagName: "video", properties: { ...sizeProperties(node), src: node.target, title: node.label ?? undefined, },
-
@@ -154,6 +169,7 @@ return {type: "element", tagName: "iframe", properties: { ...sizeProperties(node), src: node.target, title: node.label ?? undefined, },
-