-
Notifications
You must be signed in to change notification settings - Fork 44
feat(web): allow JS in template, absolute URL support #751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9c9eff3
feat(web): allow JS in template, absolute URL support
avivkeller 9add0be
fix test
avivkeller bb9def3
fix test
avivkeller 1db920f
fixup!
avivkeller 57d7dd0
fixup!
avivkeller acf914c
add docs
avivkeller f4c6dd9
fix(web): preserve trailing slash in template root
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { relative } from '../../../../utils/url.mjs'; | ||
|
|
||
| import { useAbsoluteURLs, baseURL } from '#theme/config'; | ||
|
|
||
| /** | ||
| * Returns an absolute URL (based on baseURL) or a relative URL, | ||
| * depending on the useAbsoluteURLs configuration option. | ||
| * | ||
| * @param {string} to - Target path (e.g., '/fs', '/orama-db.json') | ||
| * @param {string} from - Current page path (e.g., '/api/fs') | ||
| * @returns {string} | ||
| */ | ||
| export const relativeOrAbsolute = (to, from) => | ||
| useAbsoluteURLs | ||
| ? new URL(`.${to}`, baseURL.replace(/\/?$/, '/')).href | ||
| : relative(to, from); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| import { populateWithEvaluation } from '../processing.mjs'; | ||
|
|
||
| describe('populateWithEvaluation', () => { | ||
| it('substitutes simple ${variable} placeholders', () => { | ||
| const result = populateWithEvaluation('Hello ${name}!', { name: 'World' }); | ||
| assert.strictEqual(result, 'Hello World!'); | ||
| }); | ||
|
|
||
| it('supports multiple variables', () => { | ||
| const result = populateWithEvaluation('${greeting} ${name}!', { | ||
| greeting: 'Hi', | ||
| name: 'Node', | ||
| }); | ||
| assert.strictEqual(result, 'Hi Node!'); | ||
| }); | ||
|
|
||
| it('supports JavaScript expressions', () => { | ||
| const result = populateWithEvaluation('${value > 5 ? "big" : "small"}', { | ||
| value: 10, | ||
| }); | ||
| assert.strictEqual(result, 'big'); | ||
| }); | ||
|
|
||
| it('supports ternary expressions for conditional content', () => { | ||
| const result = populateWithEvaluation( | ||
| '${showExtra ? "extra content" : ""}', | ||
| { showExtra: false } | ||
| ); | ||
| assert.strictEqual(result, ''); | ||
| }); | ||
|
|
||
| it('handles JSON.stringify for objects', () => { | ||
| const obj = { key: 'value' }; | ||
| const result = populateWithEvaluation('${JSON.stringify(data)}', { | ||
| data: obj, | ||
| }); | ||
| assert.strictEqual(result, '{"key":"value"}'); | ||
| }); | ||
|
|
||
| it('preserves surrounding HTML content', () => { | ||
| const result = populateWithEvaluation( | ||
| '<title>${title}</title><link href="${root}styles.css" />', | ||
| { title: 'Test Page', root: '../' } | ||
| ); | ||
| assert.strictEqual( | ||
| result, | ||
| '<title>Test Page</title><link href="../styles.css" />' | ||
| ); | ||
| }); | ||
|
|
||
| it('handles empty string values', () => { | ||
| const result = populateWithEvaluation('[${content}]', { content: '' }); | ||
| assert.strictEqual(result, '[]'); | ||
| }); | ||
|
|
||
| it('handles numeric values', () => { | ||
| const result = populateWithEvaluation('count: ${count}', { count: 42 }); | ||
| assert.strictEqual(result, 'count: 42'); | ||
| }); | ||
| }); |
66 changes: 66 additions & 0 deletions
66
src/generators/web/utils/__tests__/relativeOrAbsolute.test.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { beforeEach, describe, it } from 'node:test'; | ||
|
|
||
| import { | ||
| setConfig, | ||
| default as getConfig, | ||
| } from '../../../../utils/configuration/index.mjs'; | ||
| import { relativeOrAbsolute } from '../relativeOrAbsolute.mjs'; | ||
|
|
||
| await setConfig({ | ||
| version: 'v22.0.0', | ||
| changelog: [], | ||
| generators: { | ||
| web: { | ||
| useAbsoluteURLs: false, | ||
| baseURL: 'https://nodejs.org/docs', | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| describe('relativeOrAbsolute (relative mode)', () => { | ||
| beforeEach(() => { | ||
| getConfig('web').useAbsoluteURLs = false; | ||
| }); | ||
|
|
||
| it('returns a relative path from a nested page to root', () => { | ||
| const result = relativeOrAbsolute('/', '/api/fs'); | ||
| assert.strictEqual(result, '..'); | ||
| }); | ||
|
|
||
| it('returns a relative path between sibling pages', () => { | ||
| const result = relativeOrAbsolute('/http', '/fs'); | ||
| assert.strictEqual(result, 'http'); | ||
| }); | ||
|
|
||
| it('returns a relative path for a deeper target', () => { | ||
| const result = relativeOrAbsolute('/orama-db.json', '/api/fs'); | ||
| assert.strictEqual(result, '../orama-db.json'); | ||
| }); | ||
|
|
||
| it('returns "." when source and target resolve to the same path', () => { | ||
| const result = relativeOrAbsolute('/', '/'); | ||
| assert.strictEqual(result, '.'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('relativeOrAbsolute (absolute mode)', () => { | ||
| beforeEach(() => { | ||
| getConfig('web').useAbsoluteURLs = true; | ||
| }); | ||
|
|
||
| it('returns an absolute URL to root', () => { | ||
| const result = relativeOrAbsolute('/', '/api/fs'); | ||
| assert.strictEqual(result, 'https://nodejs.org/docs/'); | ||
| }); | ||
|
|
||
| it('returns an absolute URL for a page path', () => { | ||
| const result = relativeOrAbsolute('/http', '/fs'); | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert.strictEqual(result, 'https://nodejs.org/docs/http'); | ||
| }); | ||
|
|
||
| it('returns an absolute URL for a resource', () => { | ||
| const result = relativeOrAbsolute('/orama-db.json', '/api/fs'); | ||
| assert.strictEqual(result, 'https://nodejs.org/docs/orama-db.json'); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.