diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-01-10 20:56:54 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-10 20:56:54 -0400 |
commit | 13c81e08805b1de24119c589872dc594af588dcf (patch) | |
tree | f23afc29f3e5da987bb9757d042f827e5fe50eb5 /src | |
parent | 41d35c9b3c22b20cf3291563d299b9d1f347927d (diff) | |
parent | 7f6d21d480e22765f438d5a80227f89c416b770b (diff) |
Merge pull request #134 from hsmusic/url-path-cleanup
URL & path utilities cleanup
Diffstat (limited to 'src')
-rw-r--r-- | src/misc-templates.js | 15 | ||||
-rw-r--r-- | src/util/urls.js | 109 | ||||
-rw-r--r-- | src/write/bind-utilities.js | 4 | ||||
-rw-r--r-- | src/write/build-modes/live-dev-server.js | 52 | ||||
-rw-r--r-- | src/write/build-modes/static-build.js | 105 | ||||
-rw-r--r-- | src/write/page-template.js | 19 |
6 files changed, 119 insertions, 185 deletions
diff --git a/src/misc-templates.js b/src/misc-templates.js index 6659d72b..7cfdc86c 100644 --- a/src/misc-templates.js +++ b/src/misc-templates.js @@ -968,15 +968,13 @@ function unbound_generateStickyHeadingContainer({ // Footer stuff -function unbound_getFooterLocalizationLinks(pathname, { +function unbound_getFooterLocalizationLinks({ html, defaultLanguage, language, languages, + pagePath, to, - - pageSubKey, - urlArgs, }) { const links = Object.entries(languages) .filter(([code, language]) => code !== 'default' && !language.hidden) @@ -989,11 +987,12 @@ function unbound_getFooterLocalizationLinks(pathname, { href: language === defaultLanguage ? to( - 'localizedDefaultLanguage.' + pageSubKey, - ...urlArgs) + 'localizedDefaultLanguage.' + pagePath[0], + ...pagePath.slice(1)) : to( - 'localizedWithBaseDirectory.' + pageSubKey, - language.code, ...urlArgs), + 'localizedWithBaseDirectory.' + pagePath[0], + language.code, + ...pagePath.slice(1)), }, language.name))); diff --git a/src/util/urls.js b/src/util/urls.js index 4672c6a2..c2119b8d 100644 --- a/src/util/urls.js +++ b/src/util/urls.js @@ -140,18 +140,16 @@ export const thumb = { // Makes the generally-used and wiki-specialized "to" page utility. // "to" returns a relative path from the current page to the target. export function getURLsFrom({ - urls, - baseDirectory, - pageSubKey, - subdirectoryPrefix, + pagePath, + urls, }) { + const pageSubKey = pagePath[0]; + const subdirectoryPrefix = getPageSubdirectoryPrefix({pagePath}); + return (targetFullKey, ...args) => { const [groupKey, subKey] = targetFullKey.split('.'); - let path = subdirectoryPrefix; - - let from; - let to; + let from, to; // When linking to *outside* the localized area of the site, we need to // make sure the result is correctly relative to the 8ase directory. @@ -180,9 +178,9 @@ export function getURLsFrom({ to = targetFullKey; } - path += urls.from(from).to(to, ...args); - - return path; + return ( + subdirectoryPrefix + + urls.from(from).to(to, ...args)); }; } @@ -193,19 +191,19 @@ export function getURLsFromRoot({ baseDirectory, urls, }) { - const from = urls.from('shared.root'); + const {to} = urls.from('shared.root'); return (targetFullKey, ...args) => { const [groupKey, subKey] = targetFullKey.split('.'); return ( '/' + (groupKey === 'localized' && baseDirectory - ? from.to( + ? to( 'localizedWithBaseDirectory.' + subKey, baseDirectory, ...args ) - : from.to(targetFullKey, ...args)) + : to(targetFullKey, ...args)) ); }; } @@ -213,61 +211,46 @@ export function getURLsFromRoot({ export function getPagePathname({ baseDirectory, device = false, - fullKey, - urlArgs, + pagePath, urls, }) { - const [groupKey, subKey] = fullKey.split('.'); - - const toKey = device ? 'toDevice' : 'to'; - - return (groupKey === 'localized' && baseDirectory - ? urls - .from('shared.root')[toKey]( - 'localizedWithBaseDirectory.' + subKey, - baseDirectory, - ...urlArgs) - : urls - .from('shared.root')[toKey]( - fullKey, - ...urlArgs)); -} + const {[device ? 'toDevice' : 'to']: to} = urls.from('shared.root'); -// Needed for the rare path arguments which themselves contains one or more -// slashes, e.g. for listings, with arguments like 'albums/by-name'. -export function getPageSubdirectoryPrefix({urlArgs}) { - return '../'.repeat(urlArgs.join('/').split('/').length - 1); + return (baseDirectory + ? to('localizedWithBaseDirectory.' + pagePath[0], baseDirectory, ...pagePath.slice(1)) + : to('localized.' + pagePath[0], ...pagePath.slice(1))); } -export function getPagePaths({ - baseDirectory, - fullKey, - outputPath, - urlArgs, +export function getPagePathnameAcrossLanguages({ + defaultLanguage, + languages, + pagePath, urls, }) { - const [groupKey, subKey] = fullKey.split('.'); - - const pathname = getPagePathname({ - baseDirectory, - device: true, - fullKey, - urlArgs, - urls, - }); - - const outputDirectory = path.join(outputPath, pathname); - - const output = { - directory: outputDirectory, - documentHTML: path.join(outputDirectory, 'index.html'), - oEmbedJSON: path.join(outputDirectory, 'oembed.json'), - }; - - return { - urlPath: [fullKey, ...urlArgs], + return withEntries(languages, entries => entries + .filter(([key, language]) => key !== 'default' && !language.hidden) + .map(([_key, language]) => [ + language.code, + getPagePathname({ + baseDirectory: + (language === defaultLanguage + ? '' + : language.code), + pagePath, + urls, + }), + ])); +} - output, - pathname, - }; +// Needed for the rare path arguments which themselves contains one or more +// slashes, e.g. for listings, with arguments like 'albums/by-name'. +export function getPageSubdirectoryPrefix({ + pagePath, +}) { + const timesNestedDeeply = (pagePath + .slice(1) // skip URL key, only check arguments + .join('/') + .split('/') + .length - 1); + return '../'.repeat(timesNestedDeeply); } diff --git a/src/write/bind-utilities.js b/src/write/bind-utilities.js index 4b037a91..6212b824 100644 --- a/src/write/bind-utilities.js +++ b/src/write/bind-utilities.js @@ -52,8 +52,10 @@ import { export function bindUtilities({ absoluteTo, + defaultLanguage, getSizeOfAdditionalFile, language, + languages, to, urls, wikiData, @@ -65,9 +67,11 @@ export function bindUtilities({ Object.assign(bound, { absoluteTo, + defaultLanguage, getSizeOfAdditionalFile, html, language, + languages, to, urls, wikiData, diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js index b6bf662b..0e2b6160 100644 --- a/src/write/build-modes/live-dev-server.js +++ b/src/write/build-modes/live-dev-server.js @@ -11,11 +11,10 @@ import {serializeThings} from '../../data/serialize.js'; import * as pageSpecs from '../../page/index.js'; import {logInfo, logWarn, progressCallAll} from '../../util/cli.js'; -import {withEntries} from '../../util/sugar.js'; import { getPagePathname, - getPageSubdirectoryPrefix, + getPagePathnameAcrossLanguages, getURLsFrom, getURLsFromRoot, } from '../../util/urls.js'; @@ -83,17 +82,13 @@ export async function go({ else if (page.type === 'redirect') servePath = page.fromPath; - const fullKey = 'localized.' + servePath[0]; - const urlArgs = servePath.slice(1); - return Object.values(languages).map(language => { const baseDirectory = language === defaultLanguage ? '' : language.code; const pathname = getPagePathname({ baseDirectory, - fullKey, - urlArgs, + pagePath: servePath, urls, }); @@ -249,12 +244,9 @@ export async function go({ } = urlToPageMap[pathnameKey]; const to = getURLsFrom({ - urls, baseDirectory, - pageSubKey: servePath[0], - subdirectoryPrefix: getPageSubdirectoryPrefix({ - urlArgs: servePath.slice(1), - }), + pagePath: servePath, + urls, }); const absoluteTo = getURLsFromRoot({ @@ -263,9 +255,6 @@ export async function go({ }); try { - const pageSubKey = servePath[0]; - const urlArgs = servePath.slice(1); - if (page.type === 'redirect') { response.writeHead(301, contentTypeHTML); @@ -280,25 +269,19 @@ export async function go({ response.writeHead(200, contentTypeHTML); - const localizedPathnames = withEntries(languages, entries => entries - .filter(([key, language]) => key !== 'default' && !language.hidden) - .map(([_key, language]) => [ - language.code, - getPagePathname({ - baseDirectory: - (language === defaultLanguage - ? '' - : language.code), - fullKey: 'localized.' + pageSubKey, - urlArgs, - urls, - }), - ])); + const localizedPathnames = getPagePathnameAcrossLanguages({ + defaultLanguage, + languages, + pagePath: servePath, + urls, + }); const bound = bindUtilities({ absoluteTo, + defaultLanguage, getSizeOfAdditionalFile, language, + languages, to, urls, wikiData, @@ -307,20 +290,13 @@ export async function go({ const pageInfo = page.page(bound); const pageHTML = generateDocumentHTML(pageInfo, { + ...bound, cachebust, - defaultLanguage, developersComment, - getThemeString: bound.getThemeString, - language, - languages, localizedPathnames, oEmbedJSONHref: null, // No oEmbed support for live dev server - pageSubKey, + pagePath: servePath, pathname, - urlArgs, - to, - transformMultiline: bound.transformMultiline, - wikiData, }); console.log(`${requestHead} [200] ${pathname}`); diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js index 90fc38ae..220e51f4 100644 --- a/src/write/build-modes/static-build.js +++ b/src/write/build-modes/static-build.js @@ -15,7 +15,7 @@ import {serializeThings} from '../../data/serialize.js'; import * as pageSpecs from '../../page/index.js'; import link from '../../util/link.js'; -import {empty, queue, withEntries} from '../../util/sugar.js'; +import {empty, queue} from '../../util/sugar.js'; import { logError, @@ -27,8 +27,7 @@ import { import { getPagePathname, - getPagePaths, - getPageSubdirectoryPrefix, + getPagePathnameAcrossLanguages, getURLsFrom, getURLsFromRoot, } from '../../util/urls.js'; @@ -265,40 +264,25 @@ export async function go({ await progressPromiseAll(`Writing ${language.code}`, queue([ ...pageWrites.map(page => () => { - const pageSubKey = page.path[0]; - const urlArgs = page.path.slice(1); - - const localizedPathnames = withEntries(languages, entries => entries - .filter(([key, language]) => key !== 'default' && !language.hidden) - .map(([_key, language]) => [ - language.code, - getPagePathname({ - baseDirectory: - (language === defaultLanguage - ? '' - : language.code), - fullKey: 'localized.' + pageSubKey, - urlArgs, - urls, - }), - ])); - - const paths = getPagePaths({ - outputPath, + const pagePath = page.path; + + const localizedPathnames = getPagePathnameAcrossLanguages({ + defaultLanguage, + languages, + pagePath, urls, + }); + const pathname = getPagePathname({ baseDirectory, - fullKey: 'localized.' + pageSubKey, - urlArgs, + pagePath, + urls, }); const to = getURLsFrom({ - urls, baseDirectory, - pageSubKey, - subdirectoryPrefix: getPageSubdirectoryPrefix({ - urlArgs: page.path.slice(1), - }), + pagePath, + urls, }); const absoluteTo = getURLsFromRoot({ @@ -308,8 +292,10 @@ export async function go({ const bound = bindUtilities({ absoluteTo, + defaultLanguage, getSizeOfAdditionalFile, language, + languages, to, urls, wikiData, @@ -325,32 +311,30 @@ export async function go({ const oEmbedJSONHref = oEmbedJSON && wikiData.wikiInfo.canonicalBase && - wikiData.wikiInfo.canonicalBase + + wikiData.wikiInfo.canonicalBase + urls .from('shared.root') - .to('shared.path', paths.pathname + 'oembed.json'); + .to('shared.path', pathname + 'oembed.json'); const pageHTML = generateDocumentHTML(pageInfo, { + ...bound, cachebust, - defaultLanguage, developersComment, - getThemeString: bound.getThemeString, - language, - languages, localizedPathnames, oEmbedJSONHref, - pageSubKey, - pathname: paths.pathname, - to, - transformMultiline: bound.transformMultiline, - urlArgs, - wikiData, + pagePath, + pathname, }); return writePage({ html: pageHTML, oEmbedJSON, - paths, + outputDirectory: path.join(outputPath, getPagePathname({ + baseDirectory, + device: true, + pagePath, + urls, + })), }); }), ...redirectWrites.map(({fromPath, toPath, title: titleFn}) => () => { @@ -358,27 +342,24 @@ export async function go({ language, }); - const from = getPagePaths({ - outputPath, - urls, - - baseDirectory, - fullKey: 'localized.' + fromPath[0], - urlArgs: fromPath.slice(1), - }); - const to = getURLsFrom({ - urls, baseDirectory, - pageSubKey: fromPath[0], - subdirectoryPrefix: getPageSubdirectoryPrefix({ - urlArgs: fromPath.slice(1), - }), + pagePath: fromPath, + urls, }); const target = to('localized.' + toPath[0], ...toPath.slice(1)); const html = generateRedirectHTML(title, target, {language}); - return writePage({html, paths: from}); + + return writePage({ + html, + outputDirectory: path.join(outputPath, getPagePathname({ + baseDirectory, + device: true, + pagePath: fromPath, + urls, + })), + }); }), ], queueSize)); }; @@ -424,15 +405,15 @@ import { async function writePage({ html, oEmbedJSON = '', - paths, + outputDirectory, }) { - await mkdir(paths.output.directory, {recursive: true}); + await mkdir(outputDirectory, {recursive: true}); await Promise.all([ - writeFile(paths.output.documentHTML, html), + writeFile(path.join(outputDirectory, 'index.html'), html), oEmbedJSON && - writeFile(paths.output.oEmbedJSON, oEmbedJSON), + writeFile(path.join(outputDirectory, 'oembed.json'), oEmbedJSON), ].filter(Boolean)); } diff --git a/src/write/page-template.js b/src/write/page-template.js index 88d81c23..6ed9fcf5 100644 --- a/src/write/page-template.js +++ b/src/write/page-template.js @@ -1,7 +1,6 @@ import chroma from 'chroma-js'; import * as html from '../util/html.js'; -import {logWarn} from '../util/cli.js'; import {getColors} from '../util/colors.js'; import { @@ -55,11 +54,10 @@ export function generateDocumentHTML(pageInfo, { languages, localizedPathnames, oEmbedJSONHref, - pageSubKey, + pagePath, pathname, to, transformMultiline, - urlArgs, wikiData, }) { const {wikiInfo} = wikiData; @@ -164,14 +162,13 @@ export function generateDocumentHTML(pageInfo, { }, footer.content), - getFooterLocalizationLinks(pathname, { + getFooterLocalizationLinks({ defaultLanguage, html, language, languages, - pageSubKey, + pagePath, to, - urlArgs, }), ]); @@ -265,11 +262,6 @@ export function generateDocumentHTML(pageInfo, { ? to('localized.home') : cur.path ? to(...cur.path) - : cur.href - ? (() => { - logWarn`Using legacy href format nav link in ${pathname}`; - return cur.href; - })() : null, }; if (attributes.href === null) { @@ -450,10 +442,9 @@ export function generateDocumentHTML(pageInfo, { { lang: language.intlCode, 'data-language-code': language.code, - 'data-url-key': 'localized.' + pageSubKey, + 'data-url-key': 'localized.' + pagePath[0], ...Object.fromEntries( - urlArgs.map((v, i) => [['data-url-value' + i], v]) - ), + pagePath.slice(1).map((v, i) => [['data-url-value' + i], v])), 'data-rebase-localized': to('localized.root'), 'data-rebase-shared': to('shared.root'), 'data-rebase-media': to('media.root'), |