From b6ccb611bcede41de5e69dd98115779e7bbcf76f Mon Sep 17 00:00:00 2001 From: Rohan Kaicker Date: Wed, 21 Sep 2022 22:58:48 -0400 Subject: [PATCH 1/7] added dist folder to .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b512c09..f06235c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +dist From 907ed1a45619518f69e455bd343d1037e07a7200 Mon Sep 17 00:00:00 2001 From: Rohan Kaicker Date: Wed, 21 Sep 2022 22:59:32 -0400 Subject: [PATCH 2/7] added test file of markdown test --- markdownTest.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 markdownTest.md diff --git a/markdownTest.md b/markdownTest.md new file mode 100644 index 0000000..ecddfe1 --- /dev/null +++ b/markdownTest.md @@ -0,0 +1,7 @@ +this line has no bolded text +this line has **bolded** text. + +this **line** has **alternating** bolded **text**. + +**this entire line should be bolded**. + From 8a82de495cbdf4cc6895bb216e2f30f7dbde8556 Mon Sep 17 00:00:00 2001 From: Rohan Kaicker Date: Wed, 21 Sep 2022 22:59:50 -0400 Subject: [PATCH 3/7] modified script to be able to parse markdown files and apply bolding --- generateHTML.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/generateHTML.js b/generateHTML.js index a7833bc..d3e8dc4 100644 --- a/generateHTML.js +++ b/generateHTML.js @@ -38,7 +38,13 @@ export function generateHTML(input) { if (path.extname(fileName) == ".txt") { - readFile(input + "/" + fileName).then(function(result) + readTextFile(input + "/" + fileName).then(function(result) + { + writeFile(fileName, result); + }) + } else if (path.extname(fileName) == ".md") + { + readMdFile(input + "/" + fileName).then(function(result) { writeFile(fileName, result); }) @@ -51,7 +57,15 @@ export function generateHTML(input) { if (path.extname(strippedInput) == ".txt") { - readFile(input).then(function(result) + readTextFile(input).then(function(result) + { + writeFile(strippedInput, result); + generateIndexHTML(strippedInput, false); + }) + } + else if (path.extname(strippedInput) == ".md") + { + readMdFile(input).then(function(result) { writeFile(strippedInput, result); generateIndexHTML(strippedInput, false); @@ -62,8 +76,8 @@ export function generateHTML(input) }) } -//this function will read a file -function readFile(input) +//this function will read a .txt file +function readTextFile(input) { return new Promise(async function(res, rej) { @@ -91,6 +105,59 @@ function readFile(input) }) } +//this function will read a .md file +function readMdFile(input) +{ + return new Promise(async function(res, rej) + { + var lineArray = []; + const theFile = fs.createReadStream(input); + const line = readline.createInterface( + { + input: theFile + } + ); + + //go through the file line by line + for await (const theLine of line) + { + if (theLine != "") + { + // regex to find opening and closing '**' in the line + // based off of stackoverflow answer found here: https://stackoverflow.com/a/2295943 + let pattern = /\*\*(?:\\.|[^\*\*])*\*\*/gm; + let matchIndexes = []; + let match; + while( match = pattern.exec(theLine) ){ + matchIndexes.push(match.index); + matchIndexes.push(pattern.lastIndex); + } + console.log(matchIndexes); + + // // make a modifiable copy of theLine + let modifiedLine = theLine; + + // replace '**' with '' and '' tags, note lines may have multiple bolded words + for (let i = 0; i < matchIndexes.length; i += 2) { + modifiedLine = modifiedLine.slice(0, matchIndexes[i]) + '' + modifiedLine.slice(matchIndexes[i] + 2, matchIndexes[i + 1] - 2) + '' + modifiedLine.slice(matchIndexes[i + 1]); + + // compensate for the added characters in the array (adding and tags and removing '**' --> 8 + 9 - 4 = 13) + for (let j = i + 2; j < matchIndexes.length; j++) { + matchIndexes[j] += 13; + } + } + + lineArray.push(modifiedLine); + } + else + { + lineArray.push("\n"); + } + } + res(lineArray); + }) +} + //this function will write to a file function writeFile(input, result) { From 97a97bbd458d27ce8629884303dc61c50ead5948 Mon Sep 17 00:00:00 2001 From: Rohan Kaicker Date: Wed, 21 Sep 2022 23:02:27 -0400 Subject: [PATCH 4/7] 0.0.2 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1accf53..b47a9c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "nan1-ssg", - "version": "0.0.1", + "version": "0.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "nan1-ssg", - "version": "0.0.1", + "version": "0.0.2", "license": "ISC", "dependencies": { "commander": "^9.4.0" diff --git a/package.json b/package.json index 85eb543..bc6df61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nan1-ssg", - "version": "0.0.1", + "version": "0.0.2", "description": "Release 0.1 of nan1-ssg", "main": "nan1-ssg.js", "scripts": { From f667740643ede8b4661fdf294f2322967053b63b Mon Sep 17 00:00:00 2001 From: Rohan Kaicker Date: Wed, 21 Sep 2022 23:04:09 -0400 Subject: [PATCH 5/7] updated Readme.md --- README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4da2b80..a20f040 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # nan1-ssg -nan1-ssg is a static site generator converting .txt files to .html files. +nan1-ssg is a static site generator converting .txt and .md files to .html files. ## Requirements @@ -38,7 +38,7 @@ nan1-ssg [-option] | -h, --help | Will display a help message, showing options and usage. | | -i , --input | Gives the tool a filename to generate HTML files with. The filename can be a file or a directory. | -The hello.txt file and Sherlock-Holmes-Selected-Stories directory is provided for testing purposes. +The hello.txt file, markdownTest.md file, and Sherlock-Holmes-Selected-Stories directory are provided for testing purposes. ## Examples @@ -47,6 +47,11 @@ The hello.txt file and Sherlock-Holmes-Selected-Stories directory is provided fo nan1-ssg -i hello.txt ``` +**For a markdown file:** +``` +nan1-ssg -i hello.md +``` + **For a directory:** ``` nan1-ssg -i Sherlock-Holmes-Selected-Stories @@ -64,6 +69,6 @@ nan1-ssg -i "file with spaces.txt" ## Features -- Generating valid HTML5 files from .txt files and placed in the dist directory +- Generating valid HTML5 files from .txt and .md files and placed in the dist directory - An index.html file is created which contain relative links to the generated HTML files - Each HTML file uses a default stylesheet to improve beauty and readability From e1a5ad68b739184247d68c43efcaf3667e02ea9d Mon Sep 17 00:00:00 2001 From: Rohan Kaicker Date: Wed, 21 Sep 2022 23:08:15 -0400 Subject: [PATCH 6/7] updated version to 0.2 --- main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.js b/main.js index 2cc7b9b..57f38b2 100644 --- a/main.js +++ b/main.js @@ -11,7 +11,7 @@ program.parse(process.argv); if (program.opts().version) { console.log("name: nan1-ssg"); - console.log("version: 0.1"); + console.log("version: 0.2"); } if (program.opts().input) From c69d1b11ddf8f06cd5b10218a70335870e84b15f Mon Sep 17 00:00:00 2001 From: Rohan Kaicker Date: Thu, 22 Sep 2022 19:34:02 -0400 Subject: [PATCH 7/7] added explanation of code, removed debugging logging statement --- generateHTML.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/generateHTML.js b/generateHTML.js index d3e8dc4..bac0b82 100644 --- a/generateHTML.js +++ b/generateHTML.js @@ -125,14 +125,17 @@ function readMdFile(input) { // regex to find opening and closing '**' in the line // based off of stackoverflow answer found here: https://stackoverflow.com/a/2295943 + + // the pattern below matches multiple sets of characters that are surrounded by '**' let pattern = /\*\*(?:\\.|[^\*\*])*\*\*/gm; let matchIndexes = []; let match; + + // the while loop below will test the regex pattern against the line, and then push the beginning and ending index of the match into the match indexes array to find the positions where to place the ... tags while( match = pattern.exec(theLine) ){ matchIndexes.push(match.index); matchIndexes.push(pattern.lastIndex); } - console.log(matchIndexes); // // make a modifiable copy of theLine let modifiedLine = theLine;