This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd.ts
63 lines (55 loc) · 1.88 KB
/
md.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Site, Meta } from "../utils/interfaces";
const path = require("path");
const fs = require("fs");
const { generateURL } = require("./url");
const frontmatterUtils = require("./frontmatter");
const MARKDOWN_FOLDER = require("./constants").MARKDOWN_FOLDER;
const TEMPLATE_FOLDER = require("./constants").TEMPLATE_FOLDER;
const MarkdownIt = require("markdown-it"),
md = (frontmatterCallback: Function) =>
new MarkdownIt({
html: true,
replaceLink: (link: string) => generateURL(link).toString(),
})
.use(require("markdown-it-replace-link"))
.use(require("markdown-it-front-matter"), frontmatterCallback);
const getSiteObject = (absoluteFilePath: string): Site => {
const relativeFileFolderToMDFolder = path.relative(
MARKDOWN_FOLDER,
path.dirname(absoluteFilePath)
);
const fileName = path.basename(absoluteFilePath, ".md");
// Meta-object
const DEFAULT_META = {
title: fileName, // Set default page title to the file name
template: "main.twig", // Set default template to render
};
let meta: Meta = DEFAULT_META;
// Read in MD file synchronously.
const mdString = fs.readFileSync(absoluteFilePath, "utf8");
const htmlOutput = md((frontmatter: string): void => {
meta = frontmatterUtils
.frontmatterLines(frontmatter)
.reduce((acc: any, line: string): object => {
acc[
frontmatterUtils.frontmatterTag(line)
] = frontmatterUtils.frontmatterValue(line);
return acc;
}, DEFAULT_META);
}).render(mdString);
return {
title: meta.title,
template: path.resolve(TEMPLATE_FOLDER, meta.template),
html: htmlOutput,
file: {
name: fileName,
abs: absoluteFilePath,
rel: relativeFileFolderToMDFolder,
},
url: generateURL(
path.relative(MARKDOWN_FOLDER, absoluteFilePath).replace(".md", "")
),
meta,
};
};
module.exports = { getSiteObject };