diff options
-rw-r--r-- | src/content/dependencies/linkPathFromMedia.js | 13 | ||||
-rw-r--r-- | src/content/dependencies/linkPathFromRoot.js | 13 | ||||
-rw-r--r-- | src/content/dependencies/linkPathFromSite.js | 13 | ||||
-rw-r--r-- | src/content/dependencies/linkTemplate.js | 6 | ||||
-rw-r--r-- | src/content/dependencies/linkThing.js | 13 | ||||
-rw-r--r-- | src/content/dependencies/transformContent.js | 57 | ||||
-rw-r--r-- | src/strings-default.json | 1 | ||||
-rw-r--r-- | tap-snapshots/test/snapshot/linkTemplate.js.test.cjs | 8 | ||||
-rw-r--r-- | tap-snapshots/test/snapshot/transformContent.js.test.cjs | 11 | ||||
-rw-r--r-- | test/snapshot/linkTemplate.js | 7 | ||||
-rw-r--r-- | test/snapshot/transformContent.js | 8 |
11 files changed, 119 insertions, 31 deletions
diff --git a/src/content/dependencies/linkPathFromMedia.js b/src/content/dependencies/linkPathFromMedia.js new file mode 100644 index 00000000..34a2b857 --- /dev/null +++ b/src/content/dependencies/linkPathFromMedia.js @@ -0,0 +1,13 @@ +export default { + contentDependencies: ['linkTemplate'], + + relations: (relation) => + ({link: relation('linkTemplate')}), + + data: (path) => + ({path}), + + generate: (data, relations) => + relations.link + .slot('path', ['media.path', data.path]), +}; diff --git a/src/content/dependencies/linkPathFromRoot.js b/src/content/dependencies/linkPathFromRoot.js new file mode 100644 index 00000000..dab3ac1f --- /dev/null +++ b/src/content/dependencies/linkPathFromRoot.js @@ -0,0 +1,13 @@ +export default { + contentDependencies: ['linkTemplate'], + + relations: (relation) => + ({link: relation('linkTemplate')}), + + data: (path) => + ({path}), + + generate: (data, relations) => + relations.link + .slot('path', ['shared.path', data.path]), +}; diff --git a/src/content/dependencies/linkPathFromSite.js b/src/content/dependencies/linkPathFromSite.js new file mode 100644 index 00000000..64676465 --- /dev/null +++ b/src/content/dependencies/linkPathFromSite.js @@ -0,0 +1,13 @@ +export default { + contentDependencies: ['linkTemplate'], + + relations: (relation) => + ({link: relation('linkTemplate')}), + + data: (path) => + ({path}), + + generate: (data, relations) => + relations.link + .slot('path', ['localized.path', data.path]), +}; diff --git a/src/content/dependencies/linkTemplate.js b/src/content/dependencies/linkTemplate.js index dab81ad3..2a9261e2 100644 --- a/src/content/dependencies/linkTemplate.js +++ b/src/content/dependencies/linkTemplate.js @@ -5,6 +5,7 @@ export default { 'appendIndexHTML', 'getColors', 'html', + 'language', 'to', ], @@ -23,6 +24,7 @@ export default { appendIndexHTML, getColors, html, + language, to, }) { let href = slots.href; @@ -64,6 +66,8 @@ export default { style, title, }, - slots.content); + (html.isBlank(slots.content) + ? language.$('misc.missingLinkContent') + : slots.content)); }, } diff --git a/src/content/dependencies/linkThing.js b/src/content/dependencies/linkThing.js index 4ebf4d76..e3e2608f 100644 --- a/src/content/dependencies/linkThing.js +++ b/src/content/dependencies/linkThing.js @@ -44,16 +44,15 @@ export default { generate(data, relations, slots, {html}) { const path = [data.pathKey, data.directory]; - let content = slots.content; - const name = (slots.preferShortName - ? data.nameShort ?? data.name - : data.name); + ? data.nameShort ?? data.name ?? null + : data.name ?? null); - if (html.isBlank(content)) { - content = name; - } + const content = + (html.isBlank(slots.content) + ? name + : slots.content); let color = null; if (slots.color === true) { diff --git a/src/content/dependencies/transformContent.js b/src/content/dependencies/transformContent.js index fef74e8c..93669cc1 100644 --- a/src/content/dependencies/transformContent.js +++ b/src/content/dependencies/transformContent.js @@ -33,7 +33,7 @@ export const replacerSpec = { value: (ref) => new Date(ref), html: (date, {html, language}) => html.tag('time', - {datetime: date.toString()}, + {datetime: date.toUTCString()}, language.formatDate(date)), }, 'flash-index': { @@ -129,9 +129,9 @@ const linkThingRelationMap = { }; const linkValueRelationMap = { - // media: 'linkPathFromMedia', - // root: 'linkPathFromRoot', - // site: 'linkPathFromSite', + media: 'linkPathFromMedia', + root: 'linkPathFromRoot', + site: 'linkPathFromSite', }; const linkIndexRelationMap = { @@ -189,14 +189,12 @@ export default { determineData: { // No value at all: this is an index link. if (!replacerValue || replacerValue === '-') { - data.toIndex = true; break determineData; } // Nothing to find: the link operates on a path or string, not a data object. if (!spec.find) { data.value = replacerValue; - data.toIndex = false; break determineData; } @@ -214,7 +212,6 @@ export default { // Something was found: the link operates on that thing. data.thing = thing; - data.toIndex = false; } const {transformName} = spec; @@ -236,8 +233,16 @@ export default { } // This will be another {type: 'tag'} node which gets processed in - // generate. - return node; + // generate. Extract replacerKey and replacerValue now, since it'd + // be a pain to deal with later. + return { + ...node, + data: { + ...node.data, + replacerKey: node.data.replacerKey.data, + replacerValue: node.data.replacerValue[0].data, + }, + }; }), }; }, @@ -273,7 +278,6 @@ export default { link: relation(name, arg), label: node.data.label, hash: node.data.hash, - toIndex: node.data.toIndex, } : getPlaceholder(node, content)); @@ -368,18 +372,29 @@ export default { return {type: 'text', data: linkNode.data}; } - const {link, label, hash, toIndex} = linkNode; + const {link, label, hash} = linkNode; + + // These are removed from the typical combined slots({})-style + // because we don't want to override slots that were already set + // by something that's wrapping the linkTemplate or linkThing + // template. + if (label) link.setSlot('content', label); + if (hash) link.setSlot('hash', hash); + + // TODO: This is obviously hacky. + let hasPreferShortNameSlot; + try { + link.getSlotDescription('preferShortName'); + hasPreferShortNameSlot = true; + } catch (error) { + hasPreferShortNameSlot = false; + } - return { - type: 'link', - data: - (toIndex - ? link.slots({content: label, hash}) - : link.slots({ - content: label, hash, - preferShortName: slots.preferShortLinkNames, - })), - }; + if (hasPreferShortNameSlot) { + link.setSlot('preferShortName', slots.preferShortLinkNames); + } + + return {type: 'link', data: link}; } case 'tag': { diff --git a/src/strings-default.json b/src/strings-default.json index 59e4a965..82a046f5 100644 --- a/src/strings-default.json +++ b/src/strings-default.json @@ -197,6 +197,7 @@ "misc.external.flash.homestuck.page": "{LINK} (page {PAGE})", "misc.external.flash.homestuck.secret": "{LINK} (secret page)", "misc.external.flash.youtube": "{LINK} (on any device)", + "misc.missingLinkContent": "(Missing link content)", "misc.nav.previous": "Previous", "misc.nav.next": "Next", "misc.nav.info": "Info", diff --git a/tap-snapshots/test/snapshot/linkTemplate.js.test.cjs b/tap-snapshots/test/snapshot/linkTemplate.js.test.cjs index a9c1b678..e9d22ba5 100644 --- a/tap-snapshots/test/snapshot/linkTemplate.js.test.cjs +++ b/tap-snapshots/test/snapshot/linkTemplate.js.test.cjs @@ -10,9 +10,13 @@ exports[`test/snapshot/linkTemplate.js TAP linkTemplate (snapshot) > fill many s ` exports[`test/snapshot/linkTemplate.js TAP linkTemplate (snapshot) > fill path slot & provide appendIndexHTML 1`] = ` -<a href="/c*lzone/myCoolPath/ham/pineapple/tomato/index.html"></a> +<a href="/c*lzone/myCoolPath/ham/pineapple/tomato/index.html">delish</a> +` + +exports[`test/snapshot/linkTemplate.js TAP linkTemplate (snapshot) > missing content 1`] = ` +<a href="banana">(Missing link content)</a> ` exports[`test/snapshot/linkTemplate.js TAP linkTemplate (snapshot) > special characters in path argument 1`] = ` -<a href="media/album-additional/homestuck-vol-1/Showtime%20(Piano%20Refrain)%20-%20%23xXxAwesomeSheetMusick%3FrxXx%23.pdf"></a> +<a href="media/album-additional/homestuck-vol-1/Showtime%20(Piano%20Refrain)%20-%20%23xXxAwesomeSheetMusick%3FrxXx%23.pdf">Damn, that's some good sheet music</a> ` diff --git a/tap-snapshots/test/snapshot/transformContent.js.test.cjs b/tap-snapshots/test/snapshot/transformContent.js.test.cjs index 44535ec0..4b4e090f 100644 --- a/tap-snapshots/test/snapshot/transformContent.js.test.cjs +++ b/tap-snapshots/test/snapshot/transformContent.js.test.cjs @@ -5,6 +5,12 @@ * Make sure to inspect the output below. Do not ignore changes! */ 'use strict' +exports[`test/snapshot/transformContent.js TAP transformContent (snapshot) > dates 1`] = ` +<p><time datetime="Thu, 13 Apr 2023 00:00:00 GMT">4/12/2023</time> Yep!</p> +<p>Very nice: <time datetime="Fri, 25 Oct 2413 03:00:00 GMT">10/25/2413</time></p> + +` + exports[`test/snapshot/transformContent.js TAP transformContent (snapshot) > inline images 1`] = ` <p><img src="snooping.png"> as USUAL...</p> <p>What do you know? <img src="cowabunga.png" width="24" height="32"></p> @@ -39,6 +45,11 @@ exports[`test/snapshot/transformContent.js TAP transformContent (snapshot) > non ` +exports[`test/snapshot/transformContent.js TAP transformContent (snapshot) > super basic string 1`] = ` +<p>Neat listing: Albums - by Date</p> + +` + exports[`test/snapshot/transformContent.js TAP transformContent (snapshot) > two text paragraphs 1`] = ` <p>Hello, world!</p> <p>Wow, this is very cool.</p> diff --git a/test/snapshot/linkTemplate.js b/test/snapshot/linkTemplate.js index 1db4582e..83d77108 100644 --- a/test/snapshot/linkTemplate.js +++ b/test/snapshot/linkTemplate.js @@ -30,6 +30,7 @@ testContentFunctions(t, 'linkTemplate (snapshot)', async (t, evaluate) => { slots: { path: ['myCoolPath', 'ham', 'pineapple', 'tomato'], + content: 'delish', }, }); @@ -41,6 +42,12 @@ testContentFunctions(t, 'linkTemplate (snapshot)', async (t, evaluate) => { 'homestuck-vol-1', 'Showtime (Piano Refrain) - #xXxAwesomeSheetMusick?rxXx#.pdf', ], + content: `Damn, that's some good sheet music`, }, }); + + evaluate.snapshot('missing content', { + name: 'linkTemplate', + slots: {href: 'banana'}, + }); }); diff --git a/test/snapshot/transformContent.js b/test/snapshot/transformContent.js index f55ca4fa..bf09a751 100644 --- a/test/snapshot/transformContent.js +++ b/test/snapshot/transformContent.js @@ -57,4 +57,12 @@ testContentFunctions(t, 'transformContent (snapshot)', async (t, evaluate) => { quickSnapshot( 'non-inline image #3', `<img src="spark.png">\nBaller.`); + + quickSnapshot( + 'dates', + `[[date:2023-04-13]] Yep!\nVery nice: [[date:25 October 2413]]`); + + quickSnapshot( + 'super basic string', + `Neat listing: [[string:listingPage.listAlbums.byDate.title]]`); }); |