Skip to content

Commit

Permalink
Merge branch 'issue-11'
Browse files Browse the repository at this point in the history
  • Loading branch information
mismathh committed Sep 28, 2023
2 parents 0a30c9d + ad63ed6 commit 3d7ac7d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
17 changes: 16 additions & 1 deletion examples/markdown_Sample.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
**Markdown** Sample File


This is an example of markdown with **bold** text.
This is an example of markdown with **bold** text.

This is `inline code` in a single line

```
This is an fenced code block
This is a second line in the fenced block
```
Dummy data here
but another `inline code`

```
This contains another set of inline code block
```

This is the last paragraph.
19 changes: 19 additions & 0 deletions src/parseCodeBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const parseCodeBlock = (lines) => {
let html = lines;

const codeBlockPattern = /```([^*]+)```/g;
html = html.replace(codeBlockPattern, (match, codeText) => {
codeBlock = `<pre>\n\t\t\t\t<code>${codeText}\n\t\t\t\t</code>\n\t\t\t<pre>`
return codeBlock;
});

const inlineCodePattern1 = /\`([^*]+)\`/g;
html = html.replace(inlineCodePattern1, (match, codeText) => {
return `<code>${codeText}</code>`;
});


return html;
};

module.exports = parseCodeBlock;
34 changes: 32 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require("path");
const { version } = require("../package.json");
const { start } = require( "repl" );
const markdownToHTML = require("./markdownToHTML.js");
const parseCodeBlock = require( "./parseCodeBlock" );

/* Help message that shows how to use tool and the options that are available */
const helpManual = () => {
Expand Down Expand Up @@ -90,10 +91,34 @@ const addHTMLMarkup = (lines, fileType) => {
let markupTitle = "";
let paragraphs = [""];
let startIndex = 0;
let codeBlockStartIndex = -1;
let codeBlockEndIndex = -1;
let pIndex = 0;

// Parse markdown tags
if (fileType === "md") {

// Loop through to check for code blocks
for (i = 0; i < body.length; i++) {
if (body[i] === ("```") && codeBlockStartIndex === -1) {
codeBlockStartIndex = i;
} else if (body[i] === ("```") && codeBlockStartIndex !== -1) {
codeBlockEndIndex = i;
}

// If code block is found, parse it and join to form one paragraph
if (codeBlockStartIndex !== -1 && codeBlockEndIndex !== -1) {
body[codeBlockStartIndex] = body.slice(codeBlockStartIndex, codeBlockEndIndex).join("\n\t\t\t\t\t") + body[codeBlockEndIndex];
for (j = codeBlockStartIndex + 1; j <= codeBlockEndIndex; j++) {
body[j] = "";
}
codeBlockStartIndex = -1;
codeBlockEndIndex = -1;
}
}

// Parse markdown to HTML tags
body = body.map(line => parseCodeBlock(line));
body = body.map(line => markdownToHTML(line));
}

Expand All @@ -116,7 +141,8 @@ const addHTMLMarkup = (lines, fileType) => {

}

if (body[startIndex] === "") {
// Move index to next line if there are multiple empty lines
while (body[startIndex] === "" && startIndex < body.length - 1) {
startIndex++;
i++;
}
Expand All @@ -127,7 +153,11 @@ const addHTMLMarkup = (lines, fileType) => {
}

let markupParagraphs = paragraphs.map((paragraph, index) => {
return `<p>${paragraph}</p>`;
if (!paragraph.startsWith("<pre>") && !paragraph.endsWith("</pre>")) {
return `<p>${paragraph}</p>`;
} else {
return paragraph;
}
});

if (markupTitle !== "") {
Expand Down

0 comments on commit 3d7ac7d

Please sign in to comment.