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 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 diff --git a/generateHTML.js b/generateHTML.js index a7833bc..bac0b82 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,62 @@ 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 + + // 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); + } + + // // 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) { 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) 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**. + 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": {