Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/generators/web/utils/__tests__/processing.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';

import { populateWithEvaluation } from '../processing.mjs';
import {
default as getConfig,
setConfig,
} from '../../../../utils/configuration/index.mjs';
import { populateWithEvaluation, resolvePageRoot } from '../processing.mjs';

await setConfig({
version: 'v22.0.0',
changelog: [],
generators: {
web: {
useAbsoluteURLs: false,
baseURL: 'https://nodejs.org/docs',
},
},
});
Comment thread
avivkeller marked this conversation as resolved.

describe('populateWithEvaluation', () => {
it('substitutes simple ${variable} placeholders', () => {
Expand Down Expand Up @@ -61,3 +76,30 @@ describe('populateWithEvaluation', () => {
assert.strictEqual(result, 'count: 42');
});
});

describe('resolvePageRoot', () => {
it('keeps relative roots for regular pages', () => {
const result = resolvePageRoot({ path: '/api/fs' });
assert.strictEqual(result, '../');
});

it('uses the server root for synthetic pages', () => {
const result = resolvePageRoot({
path: '/404',
synthetic: true,
});
assert.strictEqual(result, '/');
});

it('uses the configured base URL for synthetic pages with absolute URLs', async () => {
getConfig('web').useAbsoluteURLs = true;

const result = resolvePageRoot({
path: '/404',
synthetic: true,
});
assert.strictEqual(result, 'https://nodejs.org/docs/');

getConfig('web').useAbsoluteURLs = false;
});
});
19 changes: 15 additions & 4 deletions src/generators/web/utils/processing.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ export const populateWithEvaluation = (template, config) => {
return fn(...values);
};

/**
* @param {import('../../metadata/types').MetadataEntry} data
* @returns {string}
*/
export const resolvePageRoot = data => {
if (data.synthetic === true) {
const { baseURL, useAbsoluteURLs } = getConfig('web');
return useAbsoluteURLs ? String(baseURL).replace(/\/?$/, '/') : '/';
Comment thread
Herrtian marked this conversation as resolved.
}

const unresolvedRoot = relativeOrAbsolute('/', data.path);
return unresolvedRoot.endsWith('/') ? unresolvedRoot : `${unresolvedRoot}/`;
};

/**
* Converts JSX AST entries to server and client JavaScript code.
*
Expand Down Expand Up @@ -131,10 +145,7 @@ export async function processJSXEntries(
// Step 3: Render final HTML pages
const results = await Promise.all(
entries.map(async ({ data }) => {
const unresolvedRoot = relativeOrAbsolute('/', data.path);
const root = unresolvedRoot.endsWith('/')
? unresolvedRoot
: `${unresolvedRoot}/`;
const root = resolvePageRoot(data);

// Replace template placeholders with actual content
const renderedHtml = populateWithEvaluation(template, {
Expand Down