Skip to content

Commit

Permalink
Merge pull request #12 from TueNguyen2911/main
Browse files Browse the repository at this point in the history
Markdown support
  • Loading branch information
menghif committed Sep 24, 2021
2 parents a350599 + 72011d5 commit 92c633b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ typings/
# Nuxt.js build / generate output
.nuxt
dist

textfiles
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![oksmith-dodo-bird](https://user-images.githubusercontent.com/53121061/133529086-a9ef9617-3b50-488f-ac74-48b274eb90fd.jpg)

This Static Site Generator (SSG) command-line tool generates html files from txt files.
This Static Site Generator (SSG) command-line tool generates html files from txt and markdown (.md) files.

A single txt file or a folder containing txt files can be used as input.
All the html files will be created inside a new `./dist` folder.
Expand All @@ -27,6 +27,7 @@ Requirements to run this tool:

```
dodo-SSG --input file.txt
dodo-SSG --input file.md
dodo-SSG -i ./folder-name
dodo-SSG -i folder-name -s [CSS Stylesheet URL]
```
Expand Down
118 changes: 78 additions & 40 deletions dodo-SSG.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,51 @@ const argv = require("yargs")
.version(true, `dodo-SSG version: ${version}`)
.alias("h", "help").argv;

const writeHTMLFile = (title, body, file, fileType) => {
// if user provided a stylesheet, include stylesheet in the html
if (argv.stylesheet) {
stylesheet = `<link rel="stylesheet" href="${argv.stylesheet}">`;
}
const fullHTML = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>${title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${stylesheet}
</head>
<body>
<h1>${title}</h1>
${body}
</body>
</html>`;

// get filename without its '.txt' extension
const fileName = path.basename(file, fileType);

// write to 'dist' directory new html file
fs.writeFileSync("./dist/" + fileName + ".html", fullHTML);
}

let files = [];
let mdFiles = [];
let currentDir = "./";
let stylesheet = "";

if (fs.existsSync(argv.input)) {
// if input name is a '.txt' file, save it to files[0]
if (fs.statSync(argv.input).isFile() && argv.input.match(/.txt$/)) {
files[0] = argv.input;
if (fs.statSync(argv.input).isFile()) {
if(argv.input.match(/.txt$/))
files[0] = argv.input;
if(argv.input.match(/.md$/))
mdFiles[0] = argv.input;
}

// if input name is a directory, save all '.txt' files to files array
if (fs.statSync(argv.input).isDirectory()) {
currentDir = "./" + argv.input + "/";
files = fs.readdirSync(currentDir).filter((file) => file.match(/.txt$/));
mdFiles = fs.readdirSync(currentDir).filter((file) => file.match(/.md$/));
}
} else {
console.log("file or directory not found!");
Expand All @@ -43,43 +74,50 @@ if (fs.existsSync("./dist")) {
fs.mkdirSync("./dist", (err) => {
console.log(err);
});
//if there is md file
if(mdFiles.length > 0) {
mdFiles.forEach((file) => {

let fullMdText = fs
.readFileSync(currentDir + file, "utf-8")
.replace(/^###### (.*$)/gim, '<h6>$1</h6>')
.replace(/^##### (.*$)/gim, '<h5>$1</h5>')
.replace(/^#### (.*$)/gim, '<h4>$1</h4>')
.replace(/^### (.*$)/gim, '<h3>$1</h3>')
.replace(/^## (.*$)/gim, '<h2>$1</h2>')
.replace(/^# (.*$)/gim, '<h1>$1</h1>')
.replace(/\*\*(.*)\*\*/gim, '<b>$1</b>')
.replace(/\*(.*)\*/gim, '<i>$1</i>')
.replace(/\[(.*?)\]\((.*?)\)/gim, "<a href='$2'>$1</a>")
.replace(/^\`\`\`/gim, "")
.trim();

let htmlBody = fullMdText.split(/\r?\n\r?\n/).map((param, idx) => {
if(!param.match(/^<h\d>/gm) && !param.match(/^<a/gm))
return `<p>${param.replace(/\r?\n/, " ")}</p>`;
return param;
}).join(" ");
const title = htmlBody.slice(4, htmlBody.indexOf("</h1>"));
htmlBody = htmlBody.substr(htmlBody.indexOf("</h1>", 1) + 6);
writeHTMLFile(title, htmlBody, file, ".md")
})
}
if(files.length > 0) {
files.forEach((file) => {
const fullText = fs.readFileSync(currentDir + file, "utf-8");

// get title from first line of text
const title = fullText.substr(0, fullText.indexOf("\n"));

let body = fullText
.split(/\r?\n\r?\n/)
.map((para) => `\t<p>${para.replace(/\r?\n/, " ")}</p>\n\n`)
.join(" ");

// remove first line of body
body = body.substr(body.indexOf("\n", 1));
writeHTMLFile(title, body, file, ".txt");
});
}

files.forEach((file) => {
const fullText = fs.readFileSync(currentDir + file, "utf-8");

// get title from first line of text
const title = fullText.substr(0, fullText.indexOf("\n"));

let body = fullText
.split(/\r?\n\r?\n/)
.map((para) => `\t<p>${para.replace(/\r?\n/, " ")}</p>\n\n`)
.join(" ");

// remove first line of body
body = body.substr(body.indexOf("\n", 1));

// if user provided a stylesheet, include stylesheet in the html
if (argv.stylesheet) {
stylesheet = `<link rel="stylesheet" href="${argv.stylesheet}">`;
}

const fullHTML = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>${title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${stylesheet}
</head>
<body>
<h1>${title}</h1>
${body}
</body>
</html>`;

// get filename without its '.txt' extension
const fileName = path.basename(file, ".txt");

// write to 'dist' directory new html file
fs.writeFileSync("./dist/" + fileName + ".html", fullHTML);
});

0 comments on commit 92c633b

Please sign in to comment.