Skip to content

Commit b767bc3

Browse files
committed
feat(examples): upgrade vercel example to actually render pages
BREAKING CHANGE: node version requirement bumped to 11.15.0+
1 parent 1f8215d commit b767bc3

File tree

8 files changed

+85
-25
lines changed

8 files changed

+85
-25
lines changed

examples/common/render.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const frontmatter = require('front-matter');
4+
const marked = require('marked');
5+
6+
async function render(name) {
7+
// Resolve the path to the possible markdown file
8+
const fullPathToPossibleMarkdownFile = path.resolve(
9+
__dirname,
10+
`./pages/${name}${name.endsWith('.md') ? '' : '.md'}`,
11+
);
12+
// Attempt to read the file, returning false ("no I did not handle this request") if any error occurs reading the file.
13+
let markdownFileContents = null;
14+
try {
15+
markdownFileContents = await fs.promises.readFile(fullPathToPossibleMarkdownFile, { encoding: 'utf8' });
16+
} catch (ignored) {
17+
return false;
18+
}
19+
// Parse the file first using `front-matter` and `marked`
20+
const fileFrontmatter = frontmatter(markdownFileContents);
21+
const htmlFileContents = marked(fileFrontmatter.body);
22+
// Generate the HTML, using the frontmatter to generate the title.
23+
return `<html lang="en"><head><title>${fileFrontmatter.attributes.title || ''}</title>`
24+
+ `</head><body>\n${htmlFileContents}</body></html>`;
25+
}
26+
27+
/**
28+
* @returns {Promise<[string, string][]>}
29+
*/
30+
async function renderAllPages() {
31+
const fullPathToPages = path.resolve(
32+
__dirname,
33+
`./pages`,
34+
);
35+
const filesInPagesDir = await fs.promises.readdir(fullPathToPages, { encoding: 'utf8', withFileTypes: true });
36+
return Promise.all(
37+
filesInPagesDir
38+
.reduce((entries, dirEnt) => {
39+
if (dirEnt.isFile() && dirEnt.name.endsWith('.md')) {
40+
entries.push(
41+
render(dirEnt.name)
42+
.then((html) => [
43+
dirEnt.name.replace(/\.md$/i, '.html'),
44+
html,
45+
])
46+
);
47+
}
48+
return entries;
49+
}, []),
50+
);
51+
}
52+
53+
module.exports = { render, renderAllPages };

examples/generic/index.js

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const http = require('http');
22
const fs = require('fs');
33
const path = require('path');
4-
const frontmatter = require('front-matter');
5-
const marked = require('marked');
4+
const { render } = require('../common/render'); // eslint-disable-line node/no-unpublished-require
65
const netlifyCmsOAuth = require('netlify-cms-oauth-provider-node');
76

87
const port = process.env.PORT || 3000;
@@ -134,25 +133,8 @@ const handleAdmin = createStaticFileHandler('admin.html', 'text/html; charset=ut
134133
* otherwise.
135134
*/
136135
async function handlePage(req, res, { route }) {
137-
// Resolve the path to the possible markdown file
138-
const fullPathToPossibleMarkdownFile = path.resolve(
139-
__dirname,
140-
'../common/pages',
141-
`${route.replace(/^\//, '')}.md`,
142-
);
143-
// Attempt to read the file, returning false ("no I did not handle this request") if any error occurs reading the file.
144-
let markdownFileContents = null;
145-
try {
146-
markdownFileContents = await fs.promises.readFile(fullPathToPossibleMarkdownFile, { encoding: 'utf8' });
147-
} catch (ignored) {
148-
return false;
149-
}
150-
// Parse the file first using `front-matter` and `marked`
151-
const fileFrontmatter = frontmatter(markdownFileContents);
152-
const htmlFileContents = marked(fileFrontmatter.body);
153-
// Generate the HTML, using the frontmatter to generate the title.
154-
const finalHtml = `<html lang="en"><head><title>${fileFrontmatter.attributes.title || ''}</title>`
155-
+ `</head><body>\n${htmlFileContents}</body></html>`;
136+
const pageName = route.replace(/^\//, '');
137+
const finalHtml = await render(pageName);
156138
// Serve the HTML to the user.
157139
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
158140
res.end(finalHtml);
File renamed without changes.

examples/vercel/build.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { renderAllPages } = require('../common/render'); // eslint-disable-line node/no-unpublished-require
4+
5+
renderAllPages()
6+
.then((pages) => Promise.all(pages.map(async ([fileName, html]) => {
7+
const finalFileName = fileName === 'home.html' ? 'index.html' : fileName;
8+
const publicPathForFile = path.resolve(__dirname, './public', finalFileName);
9+
console.log(`Writing ${finalFileName} to ${publicPathForFile}`);
10+
await fs.promises.writeFile(publicPathForFile, html);
11+
})));

examples/vercel/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
"scripts": {
55
"start": "vercel dev",
66
"clean": "rm -rf public/",
7-
"build": "yarn run clean && mkdir public && cp index.html config.yml public/"
7+
"build": "yarn run clean && mkdir public && cp admin.html config.yml public/ && node ./build.js"
88
},
99
"engines": {
10-
"node": "10.x"
10+
"node": "14.x"
1111
},
1212
"dependencies": {
13+
"front-matter": "^4.0.2",
14+
"marked": "^2.0.1",
1315
"netlify-cms-oauth-provider-node": "latest"
1416
}
1517
}

examples/vercel/vercel.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cleanUrls": true
3+
}

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@
4646
"eslint-plugin-node": "^11.1.0",
4747
"eslint-plugin-promise": "^4.3.1",
4848
"eslint-plugin-standard": "^5.0.0",
49+
"front-matter": "^4.0.2",
4950
"husky": "^5.1.3",
5051
"jest": "^26.6.3",
5152
"lint-staged": "^10.5.4",
53+
"marked": "^2.0.1",
5254
"prettier": "^2.2.1",
5355
"semantic-release": "^17.4.2"
5456
},
5557
"engines": {
56-
"node": ">=10.0.0"
58+
"node": ">=11.15.0"
5759
},
5860
"lint-staged": {
5961
"{*,{lib,docs}/**/*,__{tests,mocks}__/**/*}.js": [

yarn.lock

+8-1
Original file line numberDiff line numberDiff line change
@@ -3609,6 +3609,13 @@ from2@^2.1.0, from2@^2.3.0:
36093609
inherits "^2.0.1"
36103610
readable-stream "^2.0.0"
36113611

3612+
front-matter@^4.0.2:
3613+
version "4.0.2"
3614+
resolved "https://registry.yarnpkg.com/front-matter/-/front-matter-4.0.2.tgz#b14e54dc745cfd7293484f3210d15ea4edd7f4d5"
3615+
integrity sha512-I8ZuJ/qG92NWX8i5x1Y8qyj3vizhXS31OxjKDu3LKP+7/qBgfIKValiZIEwoVoJKUHlhWtYrktkxV1XsX+pPlg==
3616+
dependencies:
3617+
js-yaml "^3.13.1"
3618+
36123619
36133620
version "8.1.0"
36143621
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0"
@@ -5763,7 +5770,7 @@ marked-terminal@^4.1.1:
57635770
node-emoji "^1.10.0"
57645771
supports-hyperlinks "^2.1.0"
57655772

5766-
marked@^2.0.0:
5773+
marked@^2.0.0, marked@^2.0.1:
57675774
version "2.0.1"
57685775
resolved "https://registry.yarnpkg.com/marked/-/marked-2.0.1.tgz#5e7ed7009bfa5c95182e4eb696f85e948cefcee3"
57695776
integrity sha512-5+/fKgMv2hARmMW7DOpykr2iLhl0NgjyELk5yn92iE7z8Se1IS9n3UsFm86hFXIkvMBmVxki8+ckcpjBeyo/hw==

0 commit comments

Comments
 (0)