-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrates
examples/data/
to use Preact.
Refs #71.
- Loading branch information
Showing
4 changed files
with
79 additions
and
62 deletions.
There are no files selected for viewing
This file contains 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 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,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,60 @@ | ||
import { promises as fs } from 'fs'; | ||
import * as path from 'path'; | ||
import { PrerenderResource, renderToHtml } from '@rules_prerender/preact'; | ||
|
||
const content = `${process.env['RUNFILES']}/rules_prerender/examples/data/content`; | ||
|
||
export default async function*(): | ||
AsyncGenerator<PrerenderResource, void, void> { | ||
// Read all files under `content/` in runfiles. | ||
const entries = await fs.readdir(content, { withFileTypes: true }); | ||
const files = entries.filter((entry) => entry.isFile()); | ||
|
||
// Generate an index page which links to all posts. | ||
yield PrerenderResource.of('/index.html', renderToHtml( | ||
<html> | ||
<head> | ||
<title>Data</title> | ||
<meta charSet='utf8' /> | ||
</head> | ||
<body> | ||
<h2>Data</h2> | ||
|
||
<ul> | ||
{files.map((file) => | ||
<li> | ||
<a href={`/posts/${getBaseName(file.name)}.html`}> | ||
{getBaseName(file.name)} | ||
</a> | ||
</li> | ||
)} | ||
</ul> | ||
</body> | ||
</html> | ||
)); | ||
|
||
// Generate an HTML page with the content of each file. | ||
for (const file of files) { | ||
const baseName = getBaseName(file.name); | ||
const text = await fs.readFile(path.join(content, file.name), { | ||
encoding: 'utf8', | ||
}); | ||
|
||
yield PrerenderResource.of(`/posts/${baseName}.html`, renderToHtml( | ||
<html> | ||
<head> | ||
<title>{baseName}</title> | ||
<meta charSet="utf8" /> | ||
</head> | ||
<body> | ||
<h2>{baseName}</h2> | ||
<article>{text.trim()}</article> | ||
</body> | ||
</html> | ||
)); | ||
} | ||
} | ||
|
||
function getBaseName(fileName: string): string { | ||
return fileName.split('.').slice(0, -1).join('.'); | ||
} |