@crombolo/render 0.64.0 → 0.66.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/package.json +3 -3
- package/src/values.ts +45 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/render",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.66.0",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. TSX server rendering and the published-content model for crombolo.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@crombolo/common": "0.
|
|
39
|
-
"@crombolo/core": "0.
|
|
38
|
+
"@crombolo/common": "0.66.0",
|
|
39
|
+
"@crombolo/core": "0.66.0",
|
|
40
40
|
"@types/react": "^19.3.0"
|
|
41
41
|
}
|
|
42
42
|
}
|
package/src/values.ts
CHANGED
|
@@ -8,6 +8,8 @@ import {
|
|
|
8
8
|
type Block,
|
|
9
9
|
type BlockData,
|
|
10
10
|
keyFromReference,
|
|
11
|
+
MARKDOWN_EDITOR_ALIAS,
|
|
12
|
+
markdownToHtml,
|
|
11
13
|
readBlocks,
|
|
12
14
|
resolveDictionaryWords,
|
|
13
15
|
resolveDictionaryWordsInMarkup,
|
|
@@ -227,7 +229,8 @@ function elementFrom(data: BlockData | undefined, context?: ValueContext): Publi
|
|
|
227
229
|
editorAlias: v.editorAlias ?? '',
|
|
228
230
|
culture: v.culture ?? null,
|
|
229
231
|
segment: v.segment ?? null,
|
|
230
|
-
|
|
232
|
+
// A page's own values arrive published (`toPublishedValue`); a block's as stored.
|
|
233
|
+
value: v.editorAlias === MARKDOWN_EDITOR_ALIAS ? markdownToHtml(v.value) : v.value,
|
|
231
234
|
})),
|
|
232
235
|
},
|
|
233
236
|
context,
|
|
@@ -289,11 +292,44 @@ function safeExternalUrl(url: string): string {
|
|
|
289
292
|
|
|
290
293
|
const LOCAL_LINK = /\/?(?:\{|%7B)localLink:(?:umb:\/\/[a-z-]+\/)?([0-9a-fA-F-]{32,36})(?:\}|%7D)/g
|
|
291
294
|
|
|
295
|
+
const IMAGE_TAG = /<img\b[^>]*>/gi
|
|
296
|
+
const IMAGE_UDI = /\sdata-udi\s*=\s*(["'])umb:\/\/media\/([0-9a-fA-F-]{32,36})\1/i
|
|
297
|
+
const IMAGE_SRC = /(\ssrc\s*=\s*)(["'])([^"']*)\2/i
|
|
298
|
+
|
|
299
|
+
/** The media keys the markup's images name by `data-udi`. */
|
|
300
|
+
function imageKeys(markup: string): string[] {
|
|
301
|
+
if (!markup.includes('data-udi')) return []
|
|
302
|
+
const keys: string[] = []
|
|
303
|
+
for (const tag of markup.match(IMAGE_TAG) ?? []) {
|
|
304
|
+
const key = keyFromReference(IMAGE_UDI.exec(tag)?.[2])
|
|
305
|
+
if (key) keys.push(key)
|
|
306
|
+
}
|
|
307
|
+
return keys
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Each image's `src` set to its media item's current address, its query kept,
|
|
312
|
+
* as Umbraco's `HtmlImageSourceParser` does; one whose item is not found is left.
|
|
313
|
+
*/
|
|
314
|
+
function replaceImages(markup: string, urlOf: (key: string) => string | undefined): string {
|
|
315
|
+
return markup.replace(IMAGE_TAG, (tag) => {
|
|
316
|
+
const key = keyFromReference(IMAGE_UDI.exec(tag)?.[2])
|
|
317
|
+
const url = key ? urlOf(key) : undefined
|
|
318
|
+
if (!url) return tag
|
|
319
|
+
return tag.replace(IMAGE_SRC, (_, before: string, quote: string, src: string) => {
|
|
320
|
+
const query = src.split('?')[1]
|
|
321
|
+
return `${before}${quote}${(query ? `${url}?${query}` : url).replaceAll(quote, quote === '"' ? '%22' : '%27')}${quote}`
|
|
322
|
+
})
|
|
323
|
+
})
|
|
324
|
+
}
|
|
325
|
+
|
|
292
326
|
/**
|
|
293
327
|
* Rich text links to pages and media are stored as `/{localLink:<key>}`, so a
|
|
294
328
|
* move or rename never breaks them; they become URLs when rendered. A link to
|
|
295
329
|
* something unpublished or gone becomes `#`. Pages resolve at once; a link to
|
|
296
|
-
* media is looked up in the media cache, so markup with one is a promise.
|
|
330
|
+
* media is looked up in the media cache, so markup with one is a promise. An
|
|
331
|
+
* image names its item by `data-udi`, and is shown from the item's current
|
|
332
|
+
* file, so a replaced or moved file still shows (story 355).
|
|
297
333
|
*/
|
|
298
334
|
export function resolveLocalLinks(
|
|
299
335
|
markup: string,
|
|
@@ -309,6 +345,8 @@ export function resolveLocalLinks(
|
|
|
309
345
|
const key = keyFromReference(match[1] as string)
|
|
310
346
|
if (key && context?.media && pageUrl(key) === undefined) media.add(key)
|
|
311
347
|
}
|
|
348
|
+
const images = context?.media && context.mediaUrlOf ? imageKeys(markup) : []
|
|
349
|
+
for (const key of images) media.add(key)
|
|
312
350
|
if (media.size === 0) return replaceLinks(markup, (key) => pageUrl(key) ?? '#')
|
|
313
351
|
return Promise.all(
|
|
314
352
|
[...media].map(async (key) => [key, await context?.media?.(key)] as const),
|
|
@@ -318,7 +356,11 @@ export function resolveLocalLinks(
|
|
|
318
356
|
item && context?.mediaUrlOf ? [[key, context.mediaUrlOf(item)] as const] : [],
|
|
319
357
|
),
|
|
320
358
|
)
|
|
321
|
-
|
|
359
|
+
const linked = replaceLinks(
|
|
360
|
+
markup,
|
|
361
|
+
(key) => pageUrl(key) ?? (key ? urls.get(key) : undefined) ?? '#',
|
|
362
|
+
)
|
|
363
|
+
return images.length > 0 ? replaceImages(linked, (key) => urls.get(key)) : linked
|
|
322
364
|
})
|
|
323
365
|
}
|
|
324
366
|
|