From d57d1c71e2ad0aca9a83bf26d84b3d8744255da6 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 29 Mar 2023 18:28:21 -0300 Subject: content: linkExternal, linkExternalFlash --- src/content/dependencies/linkExternal.js | 93 ++++++++++++++++++++++ src/content/dependencies/linkExternalAsIcon.js | 11 +-- src/content/dependencies/linkExternalFlash.js | 41 ++++++++++ src/misc-templates.js | 85 -------------------- .../test/snapshot/linkExternalFlash.js.test.cjs | 26 ++++++ test/snapshot/linkExternalFlash.js | 31 ++++++++ 6 files changed, 194 insertions(+), 93 deletions(-) create mode 100644 src/content/dependencies/linkExternal.js create mode 100644 src/content/dependencies/linkExternalFlash.js create mode 100644 tap-snapshots/test/snapshot/linkExternalFlash.js.test.cjs create mode 100644 test/snapshot/linkExternalFlash.js diff --git a/src/content/dependencies/linkExternal.js b/src/content/dependencies/linkExternal.js new file mode 100644 index 0000000..6ff9bf1 --- /dev/null +++ b/src/content/dependencies/linkExternal.js @@ -0,0 +1,93 @@ +// TODO: Define these as extra dependencies and pass them somewhere +const BANDCAMP_DOMAINS = ['bc.s3m.us', 'music.solatrux.com']; +const MASTODON_DOMAINS = ['types.pl']; + +export default { + extraDependencies: ['html', 'language'], + + data(url, { + type = 'generic', + } = {}) { + const types = ['generic', 'album']; + if (!types.includes(type)) { + throw new TypeError(`Expected type to be one of ${types}`); + } + + return { + url, + type, + }; + }, + + generate(data, {html, language}) { + let isLocal; + let domain; + try { + domain = new URL(data.url).hostname; + } catch (error) { + // No support for relative local URLs yet, sorry! (I.e, local URLs must + // be absolute relative to the domain name in order to work.) + isLocal = true; + } + + const a = html.tag('a', + { + href: data.url, + class: 'nowrap', + }, + + // truly unhinged indentation here + isLocal + ? language.$('misc.external.local') + + : domain.includes('bandcamp.com') + ? language.$('misc.external.bandcamp') + + : BANDCAMP_DOMAINS.includes(domain) + ? language.$('misc.external.bandcamp.domain', {domain}) + + : MASTODON_DOMAINS.includes(domain) + ? language.$('misc.external.mastodon.domain', {domain}) + + : domain.includes('youtu') + ? data.type === 'album' + ? url.includes('list=') + ? language.$('misc.external.youtube.playlist') + : language.$('misc.external.youtube.fullAlbum') + : language.$('misc.external.youtube') + + : domain.includes('soundcloud') + ? language.$('misc.external.soundcloud') + + : domain.includes('tumblr.com') + ? language.$('misc.external.tumblr') + + : domain.includes('twitter.com') + ? language.$('misc.external.twitter') + + : domain.includes('deviantart.com') + ? language.$('misc.external.deviantart') + + : domain.includes('wikipedia.org') + ? language.$('misc.external.wikipedia') + + : domain.includes('poetryfoundation.org') + ? language.$('misc.external.poetryFoundation') + + : domain.includes('instagram.com') + ? language.$('misc.external.instagram') + + : domain.includes('patreon.com') + ? language.$('misc.external.patreon') + + : domain.includes('spotify.com') + ? language.$('misc.external.spotify') + + : domain.includes('newgrounds.com') + ? language.$('misc.external.newgrounds') + + : domain); + + return a; + } +}; diff --git a/src/content/dependencies/linkExternalAsIcon.js b/src/content/dependencies/linkExternalAsIcon.js index 4473ccb..6496d02 100644 --- a/src/content/dependencies/linkExternalAsIcon.js +++ b/src/content/dependencies/linkExternalAsIcon.js @@ -1,11 +1,6 @@ -const BANDCAMP_DOMAINS = [ - 'bc.s3m.us', - 'music.solatrux.com', -]; - -const MASTODON_DOMAINS = [ - 'types.pl', -]; +// TODO: Define these as extra dependencies and pass them somewhere +const BANDCAMP_DOMAINS = ['bc.s3m.us', 'music.solatrux.com']; +const MASTODON_DOMAINS = ['types.pl']; export default { extraDependencies: ['html', 'language', 'to'], diff --git a/src/content/dependencies/linkExternalFlash.js b/src/content/dependencies/linkExternalFlash.js new file mode 100644 index 0000000..65158ff --- /dev/null +++ b/src/content/dependencies/linkExternalFlash.js @@ -0,0 +1,41 @@ +// Note: This function is seriously hard-coded for HSMusic, with custom +// presentation of links to Homestuck flashes hosted various places. + +export default { + contentDependencies: ['linkExternal'], + extraDependencies: ['html', 'language'], + + relations(relation, url) { + return { + link: relation('linkExternal', url), + }; + }, + + data(url, flash) { + return { + url, + page: flash.page, + }; + }, + + generate(data, relations, {html, language}) { + const {link} = relations; + const {url, page} = data; + + return html.tag('span', + {class: 'nowrap'}, + + url.includes('homestuck.com') + ? isNaN(Number(page)) + ? language.$('misc.external.flash.homestuck.secret', {link}) + : language.$('misc.external.flash.homestuck.page', {link, page}) + + : url.includes('bgreco.net') + ? language.$('misc.external.flash.bgreco', {link}) + + : url.includes('youtu') + ? language.$('misc.external.flash.youtube', {link}) + + : link); + }, +}; diff --git a/src/misc-templates.js b/src/misc-templates.js index 39e62dd..651b6bc 100644 --- a/src/misc-templates.js +++ b/src/misc-templates.js @@ -248,91 +248,6 @@ function unbound_generateTrackListDividedByGroups(tracks, { ]); } -// Fancy lookin' links - -function unbound_fancifyURL(url, { - html, - language, - - album = false, -} = {}) { - let local = Symbol(); - let domain; - try { - domain = new URL(url).hostname; - } catch (error) { - // No support for relative local URLs yet, sorry! (I.e, local URLs must - // be absolute relative to the domain name in order to work.) - domain = local; - } - - return html.tag('a', - { - href: url, - class: 'nowrap', - }, - - // truly unhinged indentation here - domain === local - ? language.$('misc.external.local') - : domain.includes('bandcamp.com') - ? language.$('misc.external.bandcamp') - : BANDCAMP_DOMAINS.includes(domain) - ? language.$('misc.external.bandcamp.domain', {domain}) - : MASTODON_DOMAINS.includes(domain) - ? language.$('misc.external.mastodon.domain', {domain}) - : domain.includes('youtu') - ? album - ? url.includes('list=') - ? language.$('misc.external.youtube.playlist') - : language.$('misc.external.youtube.fullAlbum') - : language.$('misc.external.youtube') - : domain.includes('soundcloud') - ? language.$('misc.external.soundcloud') - : domain.includes('tumblr.com') - ? language.$('misc.external.tumblr') - : domain.includes('twitter.com') - ? language.$('misc.external.twitter') - : domain.includes('deviantart.com') - ? language.$('misc.external.deviantart') - : domain.includes('wikipedia.org') - ? language.$('misc.external.wikipedia') - : domain.includes('poetryfoundation.org') - ? language.$('misc.external.poetryFoundation') - : domain.includes('instagram.com') - ? language.$('misc.external.instagram') - : domain.includes('patreon.com') - ? language.$('misc.external.patreon') - : domain.includes('spotify.com') - ? language.$('misc.external.spotify') - : domain.includes('newgrounds.com') - ? language.$('misc.external.newgrounds') - : domain); -} - -function unbound_fancifyFlashURL(url, flash, { - html, - language, - - fancifyURL, -}) { - const link = fancifyURL(url); - return html.tag('span', - {class: 'nowrap'}, - url.includes('homestuck.com') - ? isNaN(Number(flash.page)) - ? language.$('misc.external.flash.homestuck.secret', {link}) - : language.$('misc.external.flash.homestuck.page', { - link, - page: flash.page, - }) - : url.includes('bgreco.net') - ? language.$('misc.external.flash.bgreco', {link}) - : url.includes('youtu') - ? language.$('misc.external.flash.youtube', {link}) - : link); -} - // Grids function unbound_getGridHTML({ diff --git a/tap-snapshots/test/snapshot/linkExternalFlash.js.test.cjs b/tap-snapshots/test/snapshot/linkExternalFlash.js.test.cjs new file mode 100644 index 0000000..e4d001a --- /dev/null +++ b/tap-snapshots/test/snapshot/linkExternalFlash.js.test.cjs @@ -0,0 +1,26 @@ +/* IMPORTANT + * This snapshot file is auto-generated, but designed for humans. + * It should be checked into source control and tracked carefully. + * Re-generate by setting TAP_SNAPSHOT=1 and running tests. + * Make sure to inspect the output below. Do not ignore changes! + */ +'use strict' +exports[`test/snapshot/linkExternalFlash.js TAP linkExternalFlash (snapshot) > output 1`] = ` +homestuck.com (page 4109) +` + +exports[`test/snapshot/linkExternalFlash.js TAP linkExternalFlash (snapshot) > output 2`] = ` +homestuck.com (secret page) +` + +exports[`test/snapshot/linkExternalFlash.js TAP linkExternalFlash (snapshot) > output 3`] = ` +YouTube (on any device) +` + +exports[`test/snapshot/linkExternalFlash.js TAP linkExternalFlash (snapshot) > output 4`] = ` +www.bgreco.net (HQ Audio) +` + +exports[`test/snapshot/linkExternalFlash.js TAP linkExternalFlash (snapshot) > output 5`] = ` +Newgrounds +` diff --git a/test/snapshot/linkExternalFlash.js b/test/snapshot/linkExternalFlash.js new file mode 100644 index 0000000..3f63760 --- /dev/null +++ b/test/snapshot/linkExternalFlash.js @@ -0,0 +1,31 @@ +import t from 'tap'; +import {testContentFunctions} from '../lib/content-function.js'; + +testContentFunctions(t, 'linkExternalFlash (snapshot)', async (t, evaluate) => { + await evaluate.load(); + + evaluate.snapshot({ + name: 'linkExternalFlash', + args: ['https://homestuck.com/story/4109/', {page: '4109'}], + }); + + evaluate.snapshot({ + name: 'linkExternalFlash', + args: ['https://homestuck.com/story/pony/', {page: 'pony'}], + }); + + evaluate.snapshot({ + name: 'linkExternalFlash', + args: ['https://youtu.be/FDt-SLyEcjI', {page: '4109'}], + }); + + evaluate.snapshot({ + name: 'linkExternalFlash', + args: ['https://www.bgreco.net/hsflash/006009.html', {page: '4109'}], + }); + + evaluate.snapshot({ + name: 'linkExternalFlash', + args: ['https://www.newgrounds.com/portal/view/582345', {page: '4109'}], + }) +}); -- cgit 1.3.0-6-gf8a5