forked from shadowfacts/shadowfacts.net
Cleanup EJS rendering code
This commit is contained in:
parent
167e60d852
commit
6bb3514f6c
|
@ -19,9 +19,7 @@ async function generate(theme: string) {
|
|||
const filename = `site/css/${theme}.scss`;
|
||||
|
||||
let sass = (await fs.readFile(filename)).toString();
|
||||
sass = ejs.render(sass, {}, {
|
||||
filename: filename
|
||||
});
|
||||
sass = util.render(sass, {}, filename);
|
||||
const result = await renderSass(sass);
|
||||
|
||||
util.write(`css/${theme}.css`, result.css);
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import ejs from "ejs";
|
||||
import * as metadata from "../metadata";
|
||||
import layout from "../layout";
|
||||
import * as util from "../util";
|
||||
|
@ -6,11 +5,9 @@ import * as util from "../util";
|
|||
export default async function missing() {
|
||||
const page = await metadata.get("site/404.html.ejs");
|
||||
|
||||
page.text = ejs.render(page.text, {
|
||||
page.text = util.render(page.text, {
|
||||
metadata: page.metadata
|
||||
}, {
|
||||
filename: "site/404.html.ejs"
|
||||
});
|
||||
}, "site/404.html.ejs");
|
||||
|
||||
page.text = await layout(page.text, page.metadata, page.metadata.layout!);
|
||||
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
import path from "path";
|
||||
import ejs from "ejs";
|
||||
import formatDate from "date-fns/format";
|
||||
import * as util from "../util";
|
||||
import * as metadata from "../metadata";
|
||||
import { Page, PostMetadata } from "../metadata";
|
||||
|
@ -29,15 +27,11 @@ export default async function generatePaginted(posts: Page[], basePath: string,
|
|||
total: chunks.length,
|
||||
prevLink: pageNum == 1 ? "" : pageNum == 2 ? basePath : path.join(basePath, (pageNum - 1).toString()),
|
||||
nextLink: pageNum == chunks.length ? "" : path.join(basePath, (pageNum + 1).toString())
|
||||
},
|
||||
formatDate,
|
||||
fancyLink: util.fancyLink
|
||||
}
|
||||
};
|
||||
if (extraData) data = {...data, ...extraData};
|
||||
|
||||
let renderedTemplate = ejs.render(page.text, data, {
|
||||
filename: templatePath
|
||||
});
|
||||
let renderedTemplate = util.render(page.text, data, templatePath);
|
||||
|
||||
renderedTemplate = await layout(renderedTemplate, page.metadata, page.metadata.layout!);
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import ejs from "ejs";
|
||||
import * as metadata from "../metadata";
|
||||
import { Page } from "../metadata";
|
||||
import layout from "../layout";
|
||||
|
@ -7,12 +6,10 @@ import * as util from "../util";
|
|||
async function generateRedirect(oldPermalink: string, newPermalink: string) {
|
||||
const page = await metadata.get("site/redirect.html.ejs");
|
||||
|
||||
page.text = ejs.render(page.text, {
|
||||
page.text = util.render(page.text, {
|
||||
metadata: page.metadata,
|
||||
newPermalink
|
||||
}, {
|
||||
filename: "site/redirect.html.ejs"
|
||||
});
|
||||
}, "site/redirect.html.ejs");
|
||||
|
||||
page.text = await layout(page.text, page.metadata, page.metadata.layout!);
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { promises as fs } from "fs";
|
||||
import ejs from "ejs";
|
||||
import path from "path";
|
||||
import { Page, PostMetadata } from "../metadata";
|
||||
import * as util from "../util";
|
||||
|
@ -15,14 +14,12 @@ async function generateFeed(posts: Page[], permalink: string, category?: string)
|
|||
const dest = path.join(permalink, "feed.xml");
|
||||
|
||||
let text = (await fs.readFile("site/feed.xml.ejs")).toString();
|
||||
text = ejs.render(text, {
|
||||
text = util.render(text, {
|
||||
posts,
|
||||
category,
|
||||
permalink,
|
||||
feedPath: dest
|
||||
}, {
|
||||
filename: "site/feed.xml.ejs"
|
||||
});
|
||||
}, "site/feed.xml.ejs");
|
||||
|
||||
util.write(dest, text);
|
||||
}
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
import { promises as fs } from "fs";
|
||||
import path from "path";
|
||||
import dateFns from "date-fns";
|
||||
import formatDate from "date-fns/format";
|
||||
import slugify from "@sindresorhus/slugify";
|
||||
import { Page, PostMetadata } from "../metadata";
|
||||
import * as metadata from "../metadata";
|
||||
import * as markdown from "../markdown";
|
||||
import layout from "../layout";
|
||||
import * as util from "../util";
|
||||
import ejs from "ejs";
|
||||
|
||||
async function generateTutorials(group: string): Promise<Page[]> {
|
||||
const tutorials: Page[] = [];
|
||||
|
@ -70,14 +68,10 @@ async function generateTutorialsAndIndex(group: string, title: string): Promise<
|
|||
(<TutorialIndexMetadata>page.metadata).group = group;
|
||||
(<TutorialIndexMetadata>page.metadata).lastUpdated = <Date>(<PostMetadata>tutorials[tutorials.length - 1].metadata).date
|
||||
|
||||
page.text = ejs.render(page.text, {
|
||||
page.text = util.render(page.text, {
|
||||
tutorials,
|
||||
metadata: page.metadata,
|
||||
formatDate,
|
||||
fancyLink: util.fancyLink
|
||||
}, {
|
||||
filename: "site/tutorial-series.html.ejs"
|
||||
});
|
||||
metadata: page.metadata
|
||||
}, "site/tutorial-series.html.ejs");
|
||||
|
||||
page.text = await layout(page.text, page.metadata, page.metadata.layout!);
|
||||
|
||||
|
@ -94,13 +88,9 @@ async function generateTutorialsAndIndex(group: string, title: string): Promise<
|
|||
async function generateIndex(allSeries: TutorialSeries[]) {
|
||||
const page = await metadata.get("site/tutorials.html.ejs");
|
||||
|
||||
page.text = ejs.render(page.text, {
|
||||
allSeries,
|
||||
formatDate,
|
||||
fancyLink: util.fancyLink
|
||||
}, {
|
||||
filename: "site/tutorials.html.ejs"
|
||||
});
|
||||
page.text = util.render(page.text, {
|
||||
allSeries
|
||||
}, "site/tutorials.html.ejs");
|
||||
|
||||
page.text = await layout(page.text, page.metadata, page.metadata.layout!);
|
||||
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
import path from "path";
|
||||
import ejs from "ejs";
|
||||
import formatDate from "date-fns/format";
|
||||
import * as metadata from "./metadata";
|
||||
import { Metadata } from "./metadata";
|
||||
import * as util from "./util";
|
||||
|
@ -8,14 +6,10 @@ import * as util from "./util";
|
|||
export default async function layout(text: string, pageMetadata: Metadata, layoutPath: string): Promise<string> {
|
||||
const layoutFile = path.join("site/layouts", layoutPath);
|
||||
let layoutPage = await metadata.get(layoutFile);
|
||||
text = ejs.render(layoutPage.text, {
|
||||
text = util.render(layoutPage.text, {
|
||||
content: text,
|
||||
metadata: pageMetadata,
|
||||
formatDate,
|
||||
fancyLink: util.fancyLink
|
||||
}, {
|
||||
filename: layoutFile,
|
||||
});
|
||||
metadata: pageMetadata
|
||||
}, layoutFile);
|
||||
if (layoutPage.metadata.layout) {
|
||||
return await layout(text, pageMetadata, layoutPage.metadata.layout);
|
||||
} else {
|
||||
|
|
10
lib/util.ts
10
lib/util.ts
|
@ -1,6 +1,8 @@
|
|||
import { promises as fs } from "fs";
|
||||
import path from "path";
|
||||
import hljs from "highlight.js";
|
||||
import ejs from "ejs";
|
||||
import formatDate from "date-fns/format";
|
||||
|
||||
export async function write(filePath: string, data: any) {
|
||||
const dest = path.join("out", filePath);
|
||||
|
@ -42,3 +44,11 @@ export function fancyLink(text: string, href: string, meta?: string): string {
|
|||
<span aria-hidden="true">}</span>
|
||||
</a>`;
|
||||
}
|
||||
|
||||
export function render(template: string, data: any, filename?: string): string {
|
||||
data.fancyLink = fancyLink;
|
||||
data.formatDate = formatDate;
|
||||
return ejs.render(template, data, {
|
||||
filename
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue