|
| 1 | +import jsonpatch from "https://esm.sh/fast-json-patch"; |
| 2 | +import * as d3 from "https://esm.sh/d3@7"; |
| 3 | +// import YML from "https://esm.sh/json-to-pretty-yaml"; timing out, obnoxious |
| 4 | +import YML from "https://cdn.skypack.dev/json-to-pretty-yaml"; |
| 5 | + |
| 6 | +const searchParams = new URLSearchParams(new URL(document.URL).search); |
| 7 | + |
| 8 | +const data = await fetch(searchParams.get("file") ?? "quarto-filter-trace.json"); |
| 9 | +const json = await data.json(); |
| 10 | + |
| 11 | +const postProcessStrings = (array) => { |
| 12 | + if (array.length === 0) { return []; } |
| 13 | + const result = [array[0]]; |
| 14 | + for (const el of array.slice(1)) { |
| 15 | + if (typeof result[result.length - 1] !== "string" || |
| 16 | + typeof el !== "string") { |
| 17 | + result.push(el); |
| 18 | + continue; |
| 19 | + } |
| 20 | + result[result.length - 1] = result[result.length - 1] + el; |
| 21 | + } |
| 22 | + if (result.length === 1 && typeof result[0] === "string") { |
| 23 | + return result[0]; |
| 24 | + } |
| 25 | + return result; |
| 26 | +} |
| 27 | + |
| 28 | +const convertListAttributes = (listAttr) => ({ |
| 29 | + start: listAttr[0], |
| 30 | + style: listAttr[1].t, |
| 31 | + delimiter: listAttr[2].t, |
| 32 | +}); |
| 33 | + |
| 34 | +const convertAttr = (attr) => `('${attr[0]}', [${attr[1].map(s => `'${s}'`).join(", ")}], [${attr[2].map(s => `'${s}'`).join(", ")}])` |
| 35 | +const convertCitation = (c => c) |
| 36 | + |
| 37 | +const convert = (data) => { |
| 38 | + if (Array.isArray(data)) { |
| 39 | + return postProcessStrings(data.map(convert).flat()); |
| 40 | + } |
| 41 | + if (typeof data === "object") { |
| 42 | + const constMap = { |
| 43 | + Space: " ", |
| 44 | + Null: null, |
| 45 | + SingleQuote: "'", |
| 46 | + DoubleQuote: '"', |
| 47 | + }; |
| 48 | + if (constMap[data.t]) { |
| 49 | + return constMap[data.t]; |
| 50 | + } |
| 51 | + if (data.t === "Str") return data.c; |
| 52 | + if (["BlockQuote", "BulletList", "Plain", "Para", "Strong", "Emph", "Underline", "Strikeout", "Quoted", "SingleQuote"].includes(data.t)) { |
| 53 | + return { |
| 54 | + t: data.t, |
| 55 | + content: convert(data.c) |
| 56 | + } |
| 57 | + } |
| 58 | + if (data.t === "Code") { |
| 59 | + return { |
| 60 | + t: data.t, |
| 61 | + attr: convertAttr(data.c[0]), |
| 62 | + text: data.c[1] |
| 63 | + } |
| 64 | + } |
| 65 | + if (data.t === "Cite") { |
| 66 | + return { |
| 67 | + t: data.t, |
| 68 | + content: convert(data.c[1]), |
| 69 | + citations: data.c[0].map(convertCitation) |
| 70 | + } |
| 71 | + } |
| 72 | + if (data.t === "Div" || data.t === "Span") { |
| 73 | + return { |
| 74 | + t: data.t, |
| 75 | + attr: convertAttr(data.c[0]), |
| 76 | + content: convert(data.c.slice(1)), |
| 77 | + } |
| 78 | + } |
| 79 | + if (data.t === "Header") { |
| 80 | + return { |
| 81 | + t: data.t, |
| 82 | + level: data.c[0], |
| 83 | + attr: convertAttr(data.c[1]), |
| 84 | + content: convert(data.c.slice(2)) |
| 85 | + } |
| 86 | + } |
| 87 | + if (data.t === "Link") { |
| 88 | + return { |
| 89 | + t: data.t, |
| 90 | + attr: convertAttr(data.c[0]), |
| 91 | + content: convert(data.c[1]), |
| 92 | + target: data.c[2][0], |
| 93 | + title: data.c[2][1] |
| 94 | + } |
| 95 | + } |
| 96 | + if (data.t === "Image") { |
| 97 | + return { |
| 98 | + t: data.t, |
| 99 | + attr: convertAttr(data.c[0]), |
| 100 | + caption: convert(data.c[1]), |
| 101 | + src: data.c[2][0], |
| 102 | + title: data.c[2][1] |
| 103 | + } |
| 104 | + } |
| 105 | + if (data.t === "CodeBlock") { |
| 106 | + return { |
| 107 | + t: data.t, |
| 108 | + attr: convertAttr(data.c[0]), |
| 109 | + text: data.c[1], |
| 110 | + } |
| 111 | + } |
| 112 | + if (data.t === "OrderedList") { |
| 113 | + return { |
| 114 | + t: data.t, |
| 115 | + listAttributes: convertListAttributes(data.c[0]), |
| 116 | + content: convert(data.c[1]) |
| 117 | + } |
| 118 | + } |
| 119 | + if (data.t === "MetaInlines" || data.t === "MetaBlocks") { |
| 120 | + return postProcessStrings(data.c.map(convert)); |
| 121 | + } |
| 122 | + if (data.t === "MetaBool") { |
| 123 | + return data.c; |
| 124 | + } |
| 125 | + if (data.t === "SoftBreak") { |
| 126 | + return ""; |
| 127 | + } |
| 128 | + |
| 129 | + if (data.t === "MetaList") { |
| 130 | + return postProcessStrings(data.c.map(convert)); |
| 131 | + } |
| 132 | + if (data.t === "MetaMap") { |
| 133 | + return convertMeta(data.c); |
| 134 | + } |
| 135 | + if (data.t === "RawBlock" || data.t === "RawInline") { |
| 136 | + return { |
| 137 | + t: data.t, |
| 138 | + format: data.c[0], |
| 139 | + text: data.c[1] |
| 140 | + } |
| 141 | + } |
| 142 | + throw new Error(`Can't handle type ${data.t}`); |
| 143 | + } |
| 144 | + return { |
| 145 | + name: "<value>", |
| 146 | + children: [] |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +const convertMeta = (meta) => { |
| 151 | + return Object.fromEntries(Object.entries(meta).map(([key, value]) => { |
| 152 | + return [key, convert(value)] |
| 153 | + })); |
| 154 | +} |
| 155 | + |
| 156 | +const convertDoc = (doc) => { |
| 157 | + return { |
| 158 | + meta: convertMeta(doc.meta), |
| 159 | + "pandoc-api-version": doc["pandoc-api-version"].join(","), |
| 160 | + blocks: doc.blocks.map(convert), |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +const drawTree = (data, summary) => { |
| 165 | + const el = d3.select("#output").append("div"); |
| 166 | + const deets = el.append("details"); |
| 167 | + if (summary) { |
| 168 | + deets.append("summary").text(summary); |
| 169 | + } |
| 170 | + deets.append("pre").text(YML.stringify(data)); |
| 171 | + return deets; |
| 172 | +} |
| 173 | + |
| 174 | +d3.select("#output").append("h2").text("Starting doc") |
| 175 | + |
| 176 | +drawTree(convertDoc(json.data[0].doc), "Doc"); |
| 177 | +let isNoOp = true; |
| 178 | + |
| 179 | +for (let i = 1; i < json.data.length; ++i) { |
| 180 | + const ops = jsonpatch.compare( |
| 181 | + convertDoc(json.data[i-1].doc), |
| 182 | + convertDoc(json.data[i].doc)); |
| 183 | + if (ops.length === 0) { |
| 184 | + d3.select("#output").append("h2").text(`Filter: ${json.data[i].state} (no op)`); |
| 185 | + if (!isNoOp) { |
| 186 | + drawTree(convertDoc(json.data[i].doc), "Doc"); |
| 187 | + isNoOp = true; |
| 188 | + } |
| 189 | + continue; |
| 190 | + } |
| 191 | + isNoOp = false; |
| 192 | + |
| 193 | + d3.select("#output").append("h2").text(`Filter: ${json.data[i].state}`); |
| 194 | + drawTree(convertDoc(json.data[i].doc), "Doc"); |
| 195 | + drawTree(ops, "Ops").style("margin-bottom", "0.1em").style("margin-top", "0.1em"); |
| 196 | +} |
0 commit comments