Skip to content

Commit

Permalink
fix: allow static html with selectors (#510)
Browse files Browse the repository at this point in the history
fixes #481
  • Loading branch information
tripodsan authored Jan 25, 2024
1 parent ce899fa commit 26533a2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
19 changes: 8 additions & 11 deletions src/utils/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,18 @@ export function getPathInfo(path) {
}
let resExt = info.extension;
if (info.selector) {
if (resExt === '.html') {
// force .plain.html as markdown resources
if (info.selector === 'plain' && resExt === '.html') {
// force .plain.html as markdown resources and remove selector from path
resExt = '.md';
fileName = `${baseName}${resExt}`;
} else {
fileName = `${baseName}.${info.selector}${resExt}`;
}
segs.push(`${baseName}.${info.selector}${info.extension}`);
} else {
segs.push(`${baseName}${resExt}`);
fileName = `${baseName}${resExt}`;
}
fileName = `${baseName}${resExt}`;
}

info.path = `/${segs.join('/')}`;
Expand All @@ -94,14 +97,8 @@ export function validatePathInfo(info) {
return false;
}

// only support empty selector or plain with html
if (info.selector) {
if (info.selector !== 'plain') {
return false;
}
return info.extension === '.html';
}
return true;
// only support selector for html
return info.selector === '' || info.extension === '.html';
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/code/my-block.selector.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body>static</body></html>
30 changes: 29 additions & 1 deletion test/html-pipe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { StaticS3Loader } from './StaticS3Loader.js';
describe('HTML Pipe Test', () => {
it('responds with 404 for invalid path', async () => {
const resp = await htmlPipe(
new PipelineState({ path: '/foo.hidden.html' }),
new PipelineState({ path: '/../etc/passwd' }),
new PipelineRequest(new URL('https://www.hlx.live/')),
);
assert.strictEqual(resp.status, 404);
Expand Down Expand Up @@ -275,4 +275,32 @@ describe('HTML Pipe Test', () => {
link: '</scripts/scripts.js>; rel=modulepreload; as=script; crossorigin=use-credentials',
});
});

it('renders static html with selector my-block.selector.html', async () => {
const s3Loader = new FileS3Loader();
const state = new PipelineState({
log: console,
s3Loader,
owner: 'adobe',
repo: 'helix-pages',
ref: 'super-test',
partition: 'live',
path: '/my-block.selector.html',
timer: {
update: () => { },
},
});
const resp = await htmlPipe(
state,
new PipelineRequest(new URL('https://www.hlx.live/')),
);
assert.strictEqual(resp.status, 200);
assert.strictEqual(resp.body, '<html><body>static</body></html>\n');
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
'access-control-allow-origin': '*',
'content-type': 'text/html; charset=utf-8',
'last-modified': 'Fri, 30 Apr 2021 03:47:18 GMT',
'x-surrogate-key': 'Tl4ey1eS4kJ2iRMt kvcvppnfHtt5omSX foo-id_metadata super-test--helix-pages--adobe_head',
});
});
});
6 changes: 3 additions & 3 deletions test/utils/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('Path Utils Test - getPathInfo', () => {
selector: 'plain',
extension: '.json',
path: '/en/header.plain.json',
resourcePath: '/en/header.json',
resourcePath: '/en/header.plain.json',
originalExtension: '.json',
originalFilename: 'header.plain.json',
originalPath: '/en/header.plain.json',
Expand Down Expand Up @@ -206,8 +206,8 @@ describe('Path Utils Test - validatePathInfo', () => {
assert.strictEqual(validatePathInfo(getPathInfo('/blog.plain.html')), true);
});

it('rejects path with html.plain.html', async () => {
assert.strictEqual(validatePathInfo(getPathInfo('/blog.html.plain.html')), false);
it('allows path with html.plain.html', async () => {
assert.strictEqual(validatePathInfo(getPathInfo('/blog.html.plain.html')), true);
});

it('rejects path with plain.json', async () => {
Expand Down

0 comments on commit 26533a2

Please sign in to comment.