Skip to content

Commit

Permalink
✨ Support for creating CNAME
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Apr 27, 2020
1 parent c53f83b commit be3d83c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
26 changes: 16 additions & 10 deletions src/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFile, readdir, mkdirp } from "fs-extra";
import { readFile, readdir, mkdirp, writeFile } from "fs-extra";
import { join, parse } from "path";
import { getConfig } from "./config";
import recursiveReadDir from "recursive-readdir";
Expand All @@ -9,6 +9,12 @@ import { getTitle } from "./parse";
import { getSiteContent } from "./content";
import { filePathtoUrl } from "./helpers";

export const createCname = async () => {
const config = await getConfig();
const distPath = await getDistPath();
if (config.cname) await writeFile(join(distPath, "CNAME"), config.cname);
};

export const getContentPath = async () => {
const config = await getConfig();
const path = config.contentDir
Expand Down Expand Up @@ -82,7 +88,7 @@ export const readContentFile = async (path: string) => {

export const listRootFiles = async () => {
const contentPath = await getContentPath();
let files = (await readdir(contentPath)).map(file =>
let files = (await readdir(contentPath)).map((file) =>
parse(file).ext ? file : `${file}/index.md`
);
const result = [];
Expand All @@ -100,10 +106,10 @@ export const listRootFiles = async () => {
export const listDirs = async () => {
const contentPath = await getContentPath();
return Array.from(
new Set((await recursiveReadDir(contentPath)).map(f => parse(f).dir))
new Set((await recursiveReadDir(contentPath)).map((f) => parse(f).dir))
)
.filter(d => d !== contentPath)
.map(d => d.replace(`${contentPath}/`, ""));
.filter((d) => d !== contentPath)
.map((d) => d.replace(`${contentPath}/`, ""));
};

export const listContentFiles = async (
Expand All @@ -117,9 +123,9 @@ export const listContentFiles = async (
let files = await recursiveReadDir(contentPath);
const config = await getConfig();
if (filterContentFiles)
files = files.filter(name =>
files = files.filter((name) =>
(config.contentFileExt || ["md"])
.map(ext => `.${ext}`)
.map((ext) => `.${ext}`)
.includes(parse(name).ext)
);
let finalFiles: string[] = [];
Expand All @@ -132,7 +138,7 @@ export const listContentFiles = async (
}
}
if (removeContentPath)
finalFiles = finalFiles.map(file => file.replace(`${contentPath}/`, ""));
finalFiles = finalFiles.map((file) => file.replace(`${contentPath}/`, ""));
return finalFiles;
};

Expand All @@ -158,7 +164,7 @@ export const getTemplatePartsList = async () => {
const defaultTemplateFiles = await readdir(defaultTemplatesPartDir);
return Array.from(
new Set([...templatePartsFiles, ...defaultTemplateFiles])
).map(i => i.replace(".html", ""));
).map((i) => i.replace(".html", ""));
});
};

Expand Down Expand Up @@ -231,7 +237,7 @@ export const getNextPrevious = async (
) => {
return cached<string>(`next-prev-${type}-${current}`, async () => {
const allContent = await getSiteContent();
const currentFile = allContent.filter(f => f.htmlPath === current);
const currentFile = allContent.filter((f) => f.htmlPath === current);
if (!currentFile.length) return "";
if (type === "next") {
const next = currentFile[0].next;
Expand Down
35 changes: 18 additions & 17 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ensureFile,
copyFile,
copy,
remove
remove,
} from "fs-extra";
import {
getDistPath,
Expand All @@ -19,7 +19,8 @@ import {
getScriptPath,
getBreadcrumbs,
getBreadcrumbsSchema,
getNextPreviousNav
getNextPreviousNav,
createCname,
} from "./files";
import { cached } from "./cache";
import { join, parse } from "path";
Expand All @@ -30,7 +31,7 @@ import {
getNavbar,
getSiteMeta,
registerPartials,
getCustomCss
getCustomCss,
} from "./data";
import { minify } from "html-minifier";
import { removeHeading, getTitle, getTags, getFilesForTag } from "./parse";
Expand Down Expand Up @@ -165,11 +166,11 @@ export const generate = async (customConfig?: StaartSiteConfig) => {
if (!config.noHome) await generatePage("index.html", await getHomeContent());
if (!config.noSitemap) await generateSitemap();
const files = (await listContentFiles()).filter(
file => !["index.md", "sitemap.md"].includes(file)
(file) => !["index.md", "sitemap.md"].includes(file)
);
const allTags = await getTags();
const tags: typeof allTags = {};
Object.keys(allTags).forEach(tag => {
Object.keys(allTags).forEach((tag) => {
tags[tag] = allTags[tag];
});
for await (const key of Object.keys(tags)) {
Expand Down Expand Up @@ -218,8 +219,8 @@ ${filesList}`;
let content = await readContentFile(file);
if (parse(file).name === "index" && !config.noContentList) {
const deepFiles = (await listContentFiles(join(file, "..")))
.filter(f => f !== "index.md")
.map(f => join(file, "..", f));
.filter((f) => f !== "index.md")
.map((f) => join(file, "..", f));
if (deepFiles.length) {
content += "\n\n" + (await getNavbar(deepFiles));
}
Expand All @@ -233,7 +234,7 @@ ${filesList}`;
schemaVersion: 1,
message: "",
label: config.shieldSchemaLabel || "docs",
color: config.shieldSchemaColor || "blueviolet"
color: config.shieldSchemaColor || "blueviolet",
};
await ensureDir(join(await getDistPath(), "shield-schema"));
const nArticles = (await listContentFiles()).length;
Expand Down Expand Up @@ -273,6 +274,7 @@ ${filesList}`;
await remove(join(await getDistPath(), "@"));
} catch (error) {}
await copyAssets();
await createCname();
};

const generateSitemap = async () => {
Expand All @@ -281,7 +283,7 @@ const generateSitemap = async () => {
let content = (await getSitemapContent()) + "\n\n" + (await getNavbar(files));
await generatePage("sitemap.html", content);
const sitemap = new SitemapStream({
hostname: config.baseUrl || "http://localhost:8080"
hostname: config.baseUrl || "http://localhost:8080",
});
for await (const file of files) {
let newFile = file;
Expand Down Expand Up @@ -321,7 +323,7 @@ const generateSitemap = async () => {
{
collapseWhitespace: true,
processScripts: ["application/ld+json"],
minifyCSS: true
minifyCSS: true,
}
)
);
Expand All @@ -347,7 +349,7 @@ const generatePage = async (path: string, content: string) => {
).toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric"
day: "numeric",
})}</time>. <a href="${
githubUrl
? `${githubUrl}/blob/master/content/${path.replace(".html", ".md")}`
Expand Down Expand Up @@ -395,10 +397,9 @@ const generatePage = async (path: string, content: string) => {
metaTitle:
path === "index.html"
? (await getSiteMeta("title", "name")) || "Staart Site"
: `${await getTitle(content, false)} · ${(await getSiteMeta(
"title",
"name"
)) || "Staart Site"}`,
: `${await getTitle(content, false)} · ${
(await getSiteMeta("title", "name")) || "Staart Site"
}`,
...attributes,
description: truncate(
path === "index.html"
Expand All @@ -410,7 +411,7 @@ const generatePage = async (path: string, content: string) => {
""
),
200
)
),
};
if (process.env.NODE_ENV) {
try {
Expand Down Expand Up @@ -444,7 +445,7 @@ const generatePage = async (path: string, content: string) => {
minify(result, {
collapseWhitespace: true,
processScripts: ["application/ld+json", "text/javascript"],
minifyCSS: true
minifyCSS: true,
}).replace("// prettier-ignore ", "")
);
};

1 comment on commit be3d83c

@vercel
Copy link

@vercel vercel bot commented on be3d83c Apr 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.