Skip to content

Commit

Permalink
Renames PrerenderResource.of() to PrerenderResource.fromHtml().
Browse files Browse the repository at this point in the history
Refs #71.

This more accurately reflects its actual usage and semantics now that `fromText()` and `fromBinary()` exist.
  • Loading branch information
dgp1130 committed Mar 13, 2023
1 parent 217ae5b commit 73f61b6
Show file tree
Hide file tree
Showing 25 changed files with 55 additions and 50 deletions.
11 changes: 6 additions & 5 deletions common/models/prerender_resource.mts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class PrerenderResource {
* @param contents A {@link SafeHtml} object to encode as a UTF-8 string.
* @returns A {@link PrerenderResource} object representing the resource.
*/
public static of(path: string, contents: SafeHtml): PrerenderResource {
public static fromHtml(path: string, contents: SafeHtml):
PrerenderResource {
if (!isSafeHtml(contents)) {
throw new Error(`Only \`SafeHtml\` objects can be used in \`*.html\` or \`*.htm\` files. Use a rendering engine like \`@rules_prerender/preact\` to render to \`SafeHtml\`.`);
}
Expand All @@ -49,7 +50,7 @@ export class PrerenderResource {
*
* @param path The path the file will be generated at relative to the final
* generated site. Must begin with a `/` character. Must *not* end in
* `.html` or `.htm`. Use {@link PrerenderResource.of} with
* `.html` or `.htm`. Use {@link PrerenderResource.fromHtml} with
* {@link SafeHtml} to generate HTML content.
* @param contents A UTF-8 encoded string to output at the given path.
* @returns A {@link PrerenderResource} object representing the resource.
Expand All @@ -60,7 +61,7 @@ export class PrerenderResource {
// constructed and there could be injection attacks within it.
if (path.endsWith('.html') || path.endsWith('.htm')) {
throw new Error(`Cannot generate a \`*.html\` or \`*.htm\` file (${
path}) from a raw string (this would be unsafe!). HTML content should be rendered to \`SafeHtml\` first, and then written to a file in \`PrerenderResource.of()\`.`);
path}) from a raw string (this would be unsafe!). HTML content should be rendered to \`SafeHtml\` first, and then written to a file in \`PrerenderResource.fromHtml()\`.`);
}

return new PrerenderResource({
Expand All @@ -75,7 +76,7 @@ export class PrerenderResource {
*
* @param path The path the file will be generated at relative to the final
* generated site. Must begin with a `/` character. Must *not* end in
* `.html` or `.htm`. Use {@link PrerenderResource.of} with
* `.html` or `.htm`. Use {@link PrerenderResource.fromHtml} with
* {@link SafeHtml} to generate HTML content.
* @param contents Binary content to associate with the given path.
* @returns A {@link PrerenderResource} object representing the resource.
Expand All @@ -89,7 +90,7 @@ export class PrerenderResource {
// constructed and there could be injection attacks within it.
if (path.endsWith('.html') || path.endsWith('.htm')) {
throw new Error(`Cannot generate a \`*.html\` or \`*.htm\` file (${
path}) from a raw string (this would be unsafe!). HTML content should be rendered to \`SafeHtml\` first, and then written to a file in \`PrerenderResource.of()\`.`);
path}) from a raw string (this would be unsafe!). HTML content should be rendered to \`SafeHtml\` first, and then written to a file in \`PrerenderResource.fromHtml()\`.`);
}

return new PrerenderResource({
Expand Down
8 changes: 4 additions & 4 deletions common/models/prerender_resource_test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ import { PrerenderResource } from './prerender_resource.mjs';
describe('PrerenderResource', () => {
describe('of()', () => {
it('returns a `PrerenderResource` from `SafeHtml` data', () => {
const res = PrerenderResource.of(
const res = PrerenderResource.fromHtml(
'/foo/bar.html', safe`<div></div>`);

expect(res.path).toBe('/foo/bar.html');
expect(new TextDecoder().decode(res.contents)).toBe('<div></div>');
});

it('throws when given non-`SafeHtml` input', () => {
expect(() => PrerenderResource.of(
expect(() => PrerenderResource.fromHtml(
'/foo/bar.html',
'unsafe HTML content' as unknown as SafeHtml,
)).toThrowError(/Only `SafeHtml` objects can be used in `\*.html` or `\*.htm` files\./);

expect(() => PrerenderResource.of(
expect(() => PrerenderResource.fromHtml(
'/foo/bar.html',
{ getHtmlAsString: () => 'unsafe HTML content' } as SafeHtml,
)).toThrowError(/Only `SafeHtml` objects can be used in `\*.html` or `\*.htm` files\./);
});

it('throws when given an invalid URL path', () => {
expect(() => PrerenderResource.of(
expect(() => PrerenderResource.fromHtml(
'does/not/start/with/a/slash.ext',
safe`Hello, World!`,
)).toThrowError(/must start with a "\/"/);
Expand Down
2 changes: 1 addition & 1 deletion examples/components/site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component } from './component.js';

/** Renders a page using components. */
export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>Components</title>
Expand Down
4 changes: 2 additions & 2 deletions examples/data/site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default async function*():
const files = entries.filter((entry) => entry.isFile());

// Generate an index page which links to all posts.
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>Data</title>
Expand Down Expand Up @@ -40,7 +40,7 @@ export default async function*():
encoding: 'utf8',
});

yield PrerenderResource.of(`/posts/${baseName}.html`, renderToHtml(
yield PrerenderResource.fromHtml(`/posts/${baseName}.html`, renderToHtml(
<html>
<head>
<title>{baseName}</title>
Expand Down
2 changes: 1 addition & 1 deletion examples/declarative_shadow_dom/site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PrerenderResource, renderToHtml } from '@rules_prerender/preact';
import { Component } from './component.js';

export default async function*(): AsyncGenerator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>Declarative Shadow DOM</title>
Expand Down
7 changes: 5 additions & 2 deletions examples/external/site.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export default function*(): Generator<PrerenderResource, void, void> {
// TODO: Migrate to Preact once we figure out how to handle
// `@rules_prerender/preact`'s peer dep in an external repository with
// manual `npm_link_package()` dependencies.
yield PrerenderResource.of('/index.html', unsafeTreatStringAsSafeHtml(`
yield PrerenderResource.fromHtml(
'/index.html',
unsafeTreatStringAsSafeHtml(`
<!DOCTYPE html>
<html>
<head>
Expand All @@ -18,5 +20,6 @@ export default function*(): Generator<PrerenderResource, void, void> {
${renderComponent()}
</body>
</html>
`.trim()));
`.trim()),
);
}
2 changes: 1 addition & 1 deletion examples/javascript/site.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Component } from './component/component.mjs';

/* Renders the page. */
export default function* () {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
h('html', {}, [
h('head', {}, [
h('meta', { charSet: 'utf8' }),
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PrerenderResource, renderToHtml } from '@rules_prerender/preact';

/** Renders the page. */
export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>Minimal</title>
Expand Down
8 changes: 4 additions & 4 deletions examples/multi_page/site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PrerenderResource, includeScript, inlineStyle, renderToHtml } from '@ru
import { ComponentChildren, VNode } from 'preact';

export default function* (): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<Page>
<h2>Multi-Page</h2>
<img src="/logo.png" />
Expand All @@ -16,17 +16,17 @@ export default function* (): Generator<PrerenderResource, void, void> {
</Page>,
));

yield PrerenderResource.of('/foo.html', renderToHtml(
yield PrerenderResource.fromHtml('/foo.html', renderToHtml(
<Page>
<h2>Foo</h2>
</Page>,
));
yield PrerenderResource.of('/bar.html', renderToHtml(
yield PrerenderResource.fromHtml('/bar.html', renderToHtml(
<Page>
<h2>Bar</h2>
</Page>,
));
yield PrerenderResource.of('/hello/world.html', renderToHtml(
yield PrerenderResource.fromHtml('/hello/world.html', renderToHtml(
<Page>
<h2>Hello, World!</h2>
</Page>,
Expand Down
2 changes: 1 addition & 1 deletion examples/preact/site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PrerenderResource, renderToHtml } from '@rules_prerender/preact';
import { Component } from './component/component.js';

export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>Preact</title>
Expand Down
2 changes: 1 addition & 1 deletion examples/resources/site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component } from './component/component.js';
import { Transitive } from './transitive/transitive.js';

export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>Resources</title>
Expand Down
6 changes: 3 additions & 3 deletions examples/script_isolation/site.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrerenderResource, includeScript, renderToHtml } from '@rules_prerender/preact';

export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>Script Isolation</title>
Expand All @@ -16,7 +16,7 @@ export default function*(): Generator<PrerenderResource, void, void> {
</html>
));

yield PrerenderResource.of('/foo.html', renderToHtml(
yield PrerenderResource.fromHtml('/foo.html', renderToHtml(
<html>
<head>
<title>Script Isolation</title>
Expand All @@ -30,7 +30,7 @@ export default function*(): Generator<PrerenderResource, void, void> {
</html>
));

yield PrerenderResource.of('/bar.html', renderToHtml(
yield PrerenderResource.fromHtml('/bar.html', renderToHtml(
<html>
<head>
<title>Script Isolation</title>
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component } from './component/component.js';

/** Render some HTML with a `<script />` tag. */
export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>Scripts</title>
Expand Down
6 changes: 3 additions & 3 deletions examples/shared_chunks/site.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrerenderResource, includeScript, renderToHtml } from '@rules_prerender/preact';

export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>Shared Chunks</title>
Expand All @@ -16,7 +16,7 @@ export default function*(): Generator<PrerenderResource, void, void> {
</html>
));

yield PrerenderResource.of('/hello.html', renderToHtml(
yield PrerenderResource.fromHtml('/hello.html', renderToHtml(
<html>
<head>
<title>Shared Chunks</title>
Expand All @@ -29,7 +29,7 @@ export default function*(): Generator<PrerenderResource, void, void> {
</html>
));

yield PrerenderResource.of('/goodbye.html', renderToHtml(
yield PrerenderResource.fromHtml('/goodbye.html', renderToHtml(
<html>
<head>
<title>Shared Chunks</title>
Expand Down
2 changes: 1 addition & 1 deletion examples/site/about/about.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { srcLink } from '../common/links.mjs';

/** Renders the about page. */
export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of(
yield PrerenderResource.fromHtml(
'/about/index.html',
renderToHtml(baseLayout('About',
<article>
Expand Down
4 changes: 2 additions & 2 deletions examples/site/blog/blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default async function*():

/** Generate and return a resource with the list of posts at the given path. */
function generatePostList(path: string, posts: PostMeta[]): PrerenderResource {
return PrerenderResource.of(path, renderToHtml(baseLayout('Blog',
return PrerenderResource.fromHtml(path, renderToHtml(baseLayout('Blog',
<article>
<Template shadowroot='open'>
{polyfillDeclarativeShadowDom()}
Expand Down Expand Up @@ -76,7 +76,7 @@ async function generatePost({ urlPath, title, fileName }: PostMeta):
});
const link = srcLink(`/examples/site/blog/posts/${fileName}`);

return PrerenderResource.of(urlPath, renderToHtml(baseLayout(title,
return PrerenderResource.fromHtml(urlPath, renderToHtml(baseLayout(title,
<article>
<Template shadowroot='open'>
{polyfillDeclarativeShadowDom()}
Expand Down
6 changes: 3 additions & 3 deletions examples/site/components/counter/counter_test_cases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Counter } from './counter_prerender.js';
/** Generates prerendered test cases of the counter. */
export default function* (): Generator<PrerenderResource, void, void> {
// Prerendered counter with an initial value of zero.
yield PrerenderResource.of('/zero.html', renderToHtml(
yield PrerenderResource.fromHtml('/zero.html', renderToHtml(
<html>
<head>
<title>Zero</title>
Expand All @@ -16,7 +16,7 @@ export default function* (): Generator<PrerenderResource, void, void> {
));

// Prerendered counter with a positive initial value (4).
yield PrerenderResource.of('/positive.html', renderToHtml(
yield PrerenderResource.fromHtml('/positive.html', renderToHtml(
<html>
<head>
<title>Positive</title>
Expand All @@ -28,7 +28,7 @@ export default function* (): Generator<PrerenderResource, void, void> {
));

// Prerendered counter with a negative initial value (-7).
yield PrerenderResource.of('/negative.html', renderToHtml(
yield PrerenderResource.fromHtml('/negative.html', renderToHtml(
<html>
<head>
<title>Negative</title>
Expand Down
2 changes: 1 addition & 1 deletion examples/site/counter/counter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Counter } from '../components/counter/counter_prerender.js';

/** Renders the counter page. */
export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of(
yield PrerenderResource.fromHtml(
'/counter/index.html',
renderToHtml(baseLayout('Counter',
<article>
Expand Down
5 changes: 3 additions & 2 deletions examples/site/home/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { repo, srcLink } from '../common/links.mjs';

/** Renders the entire home page. */
export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(baseLayout('Home',
yield PrerenderResource.fromHtml('/index.html', renderToHtml(baseLayout(
'Home',
<article>
<Template shadowroot='open'>
{polyfillDeclarativeShadowDom()}
Expand Down Expand Up @@ -78,6 +79,6 @@ export default function*(): Generator<PrerenderResource, void, void> {
of <code>rules_prerender</code>!
</p>
</Template>
</article>
</article>,
)));
}
2 changes: 1 addition & 1 deletion examples/styles/site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PrerenderResource, Template, inlineStyle, renderToHtml } from '@rules_p

/** Generates a page with an inline style in a declarative shadow root. */
export default function* (): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>Inline Styles</title>
Expand Down
2 changes: 1 addition & 1 deletion examples/testonly/site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PrerenderResource, Template, inlineStyle, includeScript, renderToHtml }
import { Component } from './component/component.js';

export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>Testonly</title>
Expand Down
2 changes: 1 addition & 1 deletion examples/tsconfig/site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PrerenderResource, renderToHtml } from '@rules_prerender/preact';
* tsconfig file used to compile this file.
*/
export default function*(foo): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', renderToHtml(
yield PrerenderResource.fromHtml('/index.html', renderToHtml(
<html>
<head>
<title>tsconfig</title>
Expand Down
Loading

0 comments on commit 73f61b6

Please sign in to comment.