- commit
- 145e5b2223c464d2cbee8eb0bc858c259d52c0f7
- parent
- 5909228a5a8a54b9038a9853915d92647b0c0bbd
- author
- Dami <dami.etoile@gmail.com>
| M | diagramzip-svg/ | 9 | +++++++-- |
| M | diagramzip/ | 15 | +++++++++------ |
| M | diagramzip/ | 7 | +++++++ |
@@ -489,8 +489,13 @@ function element(name, attributes = [], children = []) {
489489 490490 function addMetadata(root, metadata) {491491 const additions = []492- if (metadata.title) additions.push(element('title', [], [{ type: 'text', value: metadata.title }]))493- if (metadata.description) additions.push(element('desc', [], [{ type: 'text', value: metadata.description }]))492+ const directText = name => root.children.some(child => child.type !== 'text'493+ && localName(child) === name494+ && child.children.length === 1495+ && child.children[0].type === 'text'496+ && child.children[0].value === (name === 'title' ? metadata.title : metadata.description))497+ if (metadata.title && !directText('title')) additions.push(element('title', [], [{ type: 'text', value: metadata.title }]))498+ if (metadata.description && !directText('desc')) additions.push(element('desc', [], [{ type: 'text', value: metadata.description }]))494499 root.children.unshift(...additions)495500 }496501 @@ -96,10 +96,11 @@ export class PreviewController {
9696 if (clientAdapter) {9797 const clientRender = await clientAdapter.render({ type, source, options }, abortController.signal)9898 rendered = {99- body: canonicalizeSvg(clientRender.body, meta, type, clientRender.version || clientRender.build || ''),99+ body: clientRender.body,100100 identity: {101101 unit: type,102102 build: clientRender.build || clientRender.version,103+ version: clientRender.version || clientRender.build || '',103104 pipeline: Array.isArray(clientRender.pipeline) ? clientRender.pipeline : [type],104105 },105106 }@@ -109,14 +110,15 @@ export class PreviewController {
109110 rendered = await this.renderThroughUnit({ type, source, options, meta, presentation }, abortController.signal)110111 }111112 if (requestNumber !== this.requestNumber) return112- const canonical = this.normalizedSvgBlob(rendered.body)113- const appearances = supportedAppearances(rendered.body)113+ const canonicalSvg = canonicalizeSvg(rendered.body, meta, type, rendered.identity.version)114+ const canonical = this.normalizedSvgBlob(canonicalSvg)115+ const appearances = supportedAppearances(canonicalSvg)114116 const requestedAppearance = presentation.appearance ?? 'raw'115117 const appearance = appearances.includes(requestedAppearance) ? requestedAppearance : 'raw'116118 this.onAppearances(appearances, appearance)117119 const displayedSvg = appearance === 'raw'118- ? materializePresentation(rendered.body, presentation)119- : materializeSvg(rendered.body, appearance)120+ ? materializePresentation(canonicalSvg, presentation)121+ : materializeSvg(canonicalSvg, appearance)120122 const displayed = this.normalizedSvgBlob(displayedSvg)121123 this.latestRenderKey = renderKey122124 this.latestSvgBlob = canonical.blob@@ -178,11 +180,12 @@ export class PreviewController {
178180 const build = response.headers.get('X-Renderer-Build')179181 ?? response.headers.get('X-Diagram-Engine-Version')180182 ?? `${unit}-unknown`183+ const version = [response.headers.get('X-Diagram-Engine-Version'), build].filter(Boolean).join(' ')181184 const pipeline = (response.headers.get('X-Diagram-Pipeline') ?? unit)182185 .split(',')183186 .map(value => value.trim().toLowerCase())184187 .filter(Boolean)185- return { body: await response.text(), identity: { unit, build, pipeline } }188+ return { body: await response.text(), identity: { unit, build, version, pipeline } }186189 }187190 188191 retryBlockedImage() {@@ -48,6 +48,13 @@ test('produces deterministic, idempotent raw canonical SVG', () => {
4848 assert.doesNotMatch(first, /data-theme=/)4949 })5050 51+test('does not duplicate root metadata when canonical output crosses another consumer', () => {52+ const once = canonicalizeSvg(source, { title: 'A', description: 'B' }, 'pikchr')53+ const twice = canonicalizeSvg(once, { title: 'A', description: 'B' }, 'pikchr')54+ assert.equal((twice.match(/<title>/g) ?? []).length, 1)55+ assert.equal((twice.match(/<desc>/g) ?? []).length, 1)56+})57+5158 test('keeps legacy editor presentation outside the canonical render', () => {5259 const canonical = canonicalizeSvg(source, metadata, 'pikchr')5360 const displayed = materializePresentation(canonical, { background: '#ffffff', padding: 8, frame: true })