Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for Markdown #7

Merged
merged 7 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
dist
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -38,7 +38,7 @@ nan1-ssg [-option]
| -h, --help | Will display a help message, showing options and usage. |
| -i <filename>, --input <filename> | 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

Expand All @@ -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
Expand All @@ -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
78 changes: 74 additions & 4 deletions generateHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand All @@ -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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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 <strong>...</strong> tags
while( match = pattern.exec(theLine) ){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment for this while loop to explain what it does

matchIndexes.push(match.index);
matchIndexes.push(pattern.lastIndex);
}

// // make a modifiable copy of theLine
let modifiedLine = theLine;

// replace '**' with '<strong>' and '</strong>' tags, note lines may have multiple bolded words
for (let i = 0; i < matchIndexes.length; i += 2) {
modifiedLine = modifiedLine.slice(0, matchIndexes[i]) + '<strong>' + modifiedLine.slice(matchIndexes[i] + 2, matchIndexes[i + 1] - 2) + '</strong>' + modifiedLine.slice(matchIndexes[i + 1]);

// compensate for the added characters in the array (adding <strong> and </strong> 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)
{
Expand Down
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions markdownTest.md
Original file line number Diff line number Diff line change
@@ -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**.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down