Skip to content

Commit

Permalink
added support for static assets
Browse files Browse the repository at this point in the history
  • Loading branch information
minhhang107 committed Oct 28, 2021
1 parent 2e0b2e9 commit 4ffc2e9
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
/node_modules
/dist
/dist
/Sherlock Holmes Selected Stories
17 changes: 17 additions & 0 deletions bin/utils/copyAssets.js
@@ -0,0 +1,17 @@
const fs = require("fs-extra");

const copyAssets = (outputFolder, assetsInputFolder) => {
const assetsFolder = `${outputFolder}/assets`;

//create assets folder
fs.mkdirSync(assetsFolder, { recursive: true });

//copy static file into assets folder
try {
fs.copySync(assetsInputFolder, assetsFolder);
} catch (err) {
console.log(err);
}
};

module.exports = copyAssets;
39 changes: 39 additions & 0 deletions bin/utils/generateHTML.js
@@ -0,0 +1,39 @@
const path = require("path");
const createHtml = require("create-html");

const generateHTML = (inputFile, stylesheet, data, assets) => {
const doubleNewLines = data.match(/^.+(\r?\n\r?\n)\r?\n/);
const title = doubleNewLines ? doubleNewLines[0] : "";
var content =
`<h1>${title.trim()}</h1>\n\n` +
data
.slice(title.length)
.split(/(\r?\n)+3/)
.map((para) => `<p>${para.replace(/\r?\n/, " ")}</p>`)
.join("\n");

if (assets) {
const imgRegex = /\<img src="(.*?)"(.*?)\>/gim;
const imgTags = content.match(imgRegex);
imgTags.forEach((tag) => {
assets.forEach((asset) => {
const src = /src="(.*?)"/gim;
if (tag.includes(asset)) {
const updatedTag = tag.replace(src, `src="assets/${asset}"`);
content = content.replace(tag, updatedTag);
}
});
});
}

const html = createHtml({
title: title != "" ? title.trim() : path.basename(inputFile, ".txt"),
css: stylesheet,
lang: "en",
head: `<meta name="viewport" content="width=device-width, initial-scale=1"/>`,
body: content,
});
return html;
};

module.exports = generateHTML;
70 changes: 28 additions & 42 deletions bin/utils/processFile.js
@@ -1,10 +1,32 @@
const fs = require("fs");
const path = require("path");
const chalk = require("chalk");
const createHtml = require("create-html");
const generateHTML = require("./generateHTML");
const hljs = require("highlight.js");
const markdown = require("markdown-it")({
html: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return (
'<pre class="hljs"><code>' +
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
"</code></pre>"
);
} catch (__) {}
}

return (
'<pre class="hljs"><code>' +
markdown.utils.escapeHtml(str) +
"</code></pre>"
);
},
});

//Convert text file into html file, return html content
const processFile = (inputFile, output, stylesheet) => {
let assets;
const inputFilePath = path.join(path.resolve(), inputFile);
const getOutputFileName = (ext) => {
return path.join(output, path.basename(inputFile, ext) + ".html");
Expand All @@ -22,49 +44,13 @@ const processFile = (inputFile, output, stylesheet) => {
outputFilePath = getOutputFileName(".txt");
} else {
outputFilePath = getOutputFileName(".md");

const h1 = /^# (.*$)/gim;
const h2 = /^## (.*$)/gim;
const h3 = /^### (.*$)/gim;
const h4 = /^#### (.*$)/gim;
const h5 = /^##### (.*$)/gim;
const h6 = /^###### (.*$)/gim;
const bold = /\*\*(.*)\*\*/gim;
const italics = /\_(.*)\_/gim;
const link = /\[(.*?)\]\((.*?)\)/gim;
const code = /\`([^`].*)\`/gim;
const hr = /^---$/gim;
data = data
.replace(h1, "<h1>$1</h1>")
.replace(h2, "<h2>$1</h2>")
.replace(h3, "<h3>$1</h3>")
.replace(h4, "<h4>$1</h4>")
.replace(h5, "<h5>$1</h5>")
.replace(h6, "<h6>$1</h6>")
.replace(bold, "<b>$1</b>")
.replace(italics, "<i>$1</i>")
.replace(link, "<a href='$2'>$1</a>")
.replace(code, "<code>$1</code>")
.replace(hr, "<hr>");
data = markdown.render(data);
if (fs.existsSync(`${output}/assets`)) {
assets = fs.readdirSync(`${output}/assets`);
}
}

const doubleNewLines = data.match(/^.+(\r?\n\r?\n)\r?\n/);
const title = doubleNewLines ? doubleNewLines[0] : "";
const content =
`<h1>${title.trim()}</h1>\n\n` +
data
.slice(title.length)
.split(/\r?\n\r?\n/)
.map((para) => `<p>${para.replace(/\r?\n/, " ")}</p>`)
.join("\n\n");

const html = createHtml({
title: title != "" ? title.trim() : path.basename(inputFile, ".txt"),
css: stylesheet,
lang: "en",
head: `<meta name="viewport" content="width=device-width, initial-scale=1"/>`,
body: content,
});
const html = generateHTML(inputFile, stylesheet, data, assets);

fs.writeFile(outputFilePath, html, (err) => {
if (err) {
Expand Down
6 changes: 5 additions & 1 deletion bin/utils/processInput.js
Expand Up @@ -4,8 +4,9 @@ const chalk = require("chalk");
const processFile = require("./processFile");
const processFolder = require("./processFolder");
const validateOutputFolder = require("./validateOutputFolder");
const copyAssets = require("./copyAssets");

const processInput = (input, output, stylesheet) => {
const processInput = (input, output, stylesheet, assets) => {
output = !output || output == "" ? "dist" : output;

fs.lstat(input, (err, stats) => {
Expand All @@ -26,6 +27,9 @@ const processInput = (input, output, stylesheet) => {
return process.exit(1);
}

//handle assets
if (assets && assets !== "") copyAssets(output, assets);

//handle text file input
if (stats.isFile()) {
if (path.extname(input) !== ".txt" && path.extname(input) !== ".md") {
Expand Down
2 changes: 1 addition & 1 deletion bin/utils/processJSONFile.js
Expand Up @@ -9,7 +9,7 @@ const processJSONFile = (jsonFile) => {
return process.exit(1);
}

processInput(data.input, data.output, data.stylesheet);
processInput(data.input, data.output, data.stylesheet, data.assets);
});
};

Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -14,7 +14,10 @@
"dependencies": {
"chalk": "^4.1.2",
"create-html": "^4.1.0",
"fs-extra": "^10.0.0",
"highlight.js": "^11.3.1",
"jsonfile": "^6.1.0",
"markdown-it": "^12.2.0",
"yargs": "^17.1.1"
}
}

0 comments on commit 4ffc2e9

Please sign in to comment.