|
| 1 | +import { type Plugin } from "unified"; |
| 2 | +import type * as mdast from "mdast"; |
| 3 | +import { visit } from "unist-util-visit"; |
| 4 | + |
| 5 | +declare module "mdast" { |
| 6 | + interface ImageData { |
| 7 | + attr?: Record<string, string | undefined>; |
| 8 | + } |
| 9 | + interface ImageReferenceData { |
| 10 | + attr?: Record<string, string | undefined>; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +export function parseAttr( |
| 15 | + s: string, |
| 16 | +): [attr: Record<string, string | undefined>, rest: string] | undefined { |
| 17 | + const attr: Record<string, string | undefined> = {}; |
| 18 | + let status: |
| 19 | + | { |
| 20 | + type: "didn't start"; |
| 21 | + } |
| 22 | + | { |
| 23 | + type: "ended"; |
| 24 | + } |
| 25 | + | { |
| 26 | + type: "key"; |
| 27 | + buf: string[]; |
| 28 | + } |
| 29 | + | { |
| 30 | + type: "value unknown"; |
| 31 | + key: string; |
| 32 | + } |
| 33 | + | { |
| 34 | + type: |
| 35 | + | "value without quote" |
| 36 | + | "value with single quote" |
| 37 | + | "value with double quote"; |
| 38 | + key: string; |
| 39 | + buf: string[]; |
| 40 | + } |
| 41 | + | { type: "wait for next" } = { type: "didn't start" }; |
| 42 | + let len = 0; |
| 43 | + for (const c of s) { |
| 44 | + ++len; |
| 45 | + if (c === "\n" || c === "\r" || c === "\t") break; |
| 46 | + if (status.type === "didn't start") { |
| 47 | + if (c !== "{") break; |
| 48 | + status = { type: "key", buf: [] }; |
| 49 | + } else if (status.type === "key") { |
| 50 | + if (c === "=") { |
| 51 | + if (status.buf.length === 0) break; |
| 52 | + status = { |
| 53 | + type: "value unknown", |
| 54 | + key: status.buf.join("").trim(), |
| 55 | + }; |
| 56 | + } else if (c === ",") { |
| 57 | + attr[status.buf.join("").trim()] = undefined; |
| 58 | + status = { type: "key", buf: [] }; |
| 59 | + } else if (c === "}") { |
| 60 | + status = { type: "ended" }; |
| 61 | + break; |
| 62 | + } else status.buf.push(c); |
| 63 | + } else if (status.type === "value unknown") { |
| 64 | + if (c === " ") continue; |
| 65 | + else if (c === "'") |
| 66 | + status = { |
| 67 | + type: "value with single quote", |
| 68 | + key: status.key, |
| 69 | + buf: [], |
| 70 | + }; |
| 71 | + else if (c === '"') |
| 72 | + status = { |
| 73 | + type: "value with double quote", |
| 74 | + key: status.key, |
| 75 | + buf: [], |
| 76 | + }; |
| 77 | + else if (c === ",") { |
| 78 | + attr[status.key] = ""; |
| 79 | + status = { type: "key", buf: [] }; |
| 80 | + } else if (c === "}") { |
| 81 | + attr[status.key] = ""; |
| 82 | + status = { type: "ended" }; |
| 83 | + break; |
| 84 | + } else if (c === "=") break; |
| 85 | + else |
| 86 | + status = { |
| 87 | + type: "value without quote", |
| 88 | + key: status.key, |
| 89 | + buf: [c], |
| 90 | + }; |
| 91 | + } else if (status.type === "value without quote") { |
| 92 | + if (c === ",") { |
| 93 | + attr[status.key] = status.buf.join("").trim(); |
| 94 | + status = { type: "key", buf: [] }; |
| 95 | + } else if (c === "}") { |
| 96 | + attr[status.key] = status.buf.join("").trim(); |
| 97 | + status = { type: "ended" }; |
| 98 | + break; |
| 99 | + } else if (c === "=") break; |
| 100 | + else status.buf.push(c); |
| 101 | + } else if (status.type === "value with single quote") { |
| 102 | + if (c === "'") { |
| 103 | + attr[status.key] = status.buf.join(""); |
| 104 | + status = { type: "wait for next" }; |
| 105 | + } else status.buf.push(c); |
| 106 | + } else if (status.type === "value with double quote") { |
| 107 | + if (c === '"') { |
| 108 | + attr[status.key] = status.buf.join(""); |
| 109 | + status = { type: "wait for next" }; |
| 110 | + } else status.buf.push(c); |
| 111 | + } else if (status.type === "wait for next") { |
| 112 | + if (c === ",") { |
| 113 | + status = { type: "key", buf: [] }; |
| 114 | + } else if (c === "}") { |
| 115 | + status = { type: "ended" }; |
| 116 | + break; |
| 117 | + } else if (c !== " ") break; |
| 118 | + } |
| 119 | + } |
| 120 | + if (status.type !== "ended") return undefined; |
| 121 | + else return [attr, s.slice(len)]; |
| 122 | +} |
| 123 | + |
| 124 | +const remarkImageAttr: Plugin<[], mdast.Root, mdast.Root> = () => { |
| 125 | + return (tree) => { |
| 126 | + if (tree.type !== "root") |
| 127 | + throw new TypeError(`Expected root node, got ${tree.type}`); |
| 128 | + visit(tree, (node) => { |
| 129 | + if (!("children" in node)) return; |
| 130 | + for (let i = 1; i < node.children.length; ++i) { |
| 131 | + const pre = node.children[i - 1], |
| 132 | + cur = node.children[i]; |
| 133 | + if ( |
| 134 | + (pre.type !== "image" && pre.type !== "imageReference") || |
| 135 | + cur.type !== "text" |
| 136 | + ) |
| 137 | + continue; |
| 138 | + const res = parseAttr(cur.value); |
| 139 | + if (!res) continue; |
| 140 | + const [attr, rest] = res; |
| 141 | + if (!pre.data) pre.data = {}; |
| 142 | + if (!pre.data.attr) pre.data.attr = {}; |
| 143 | + Object.assign(pre.data.attr, attr); |
| 144 | + cur.value = rest; |
| 145 | + } |
| 146 | + node.children = node.children.filter((n) => n.type !== "text" || n.value); |
| 147 | + }); |
| 148 | + }; |
| 149 | +}; |
| 150 | + |
| 151 | +export default remarkImageAttr; |
0 commit comments