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 #13
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just have some minor comments here and there.
src/parse-markdown/index.ts
Outdated
@@ -22,7 +30,11 @@ export default function parseMarkdown(md: string, fname: string) { | |||
lineHtml += `<h1>${parsedParagraph}</h1>`; | |||
title = parsedParagraph; // Set <title> tag | |||
} else { | |||
lineHtml += `<p>${parsedParagraph}</p>`; | |||
if (parsedParagraph.startsWith("<h2>") && parsedParagraph.endsWith("</h2>")) { | |||
lineHtml += `${parsedParagraph}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for template syntax, can simply do:
lineHtml += parsedParagraph;
|
||
const headingTwoPattern = /^( {0,3}##\s+.*)/; | ||
html = html.replace(headingTwoPattern, (match, headingTwoText) => { | ||
let headingTwo = headingTwoText.split(/##/).slice(1).join("##").trim(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer using const
whenever possible if a value does not need to be mutated within its scope.
|
||
const headingTwoPattern2 = /^(.*\n {0,3}-+\s*)/; | ||
html = html.replace(headingTwoPattern2, (match, headingTwoText) => { | ||
let altHeadingTwo = headingTwoText.split(/\n/)[0].trim(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer using const
whenever possible if a value does not need to be mutated within its scope. I'd also prefer using the same variable headingTwo
like last time since it's a good variable name and the two scopes are distinct.
src/parse-markdown/index.ts
Outdated
@@ -9,6 +9,14 @@ export default function parseMarkdown(md: string, fname: string) { | |||
const hasTitle = | |||
paragraphs[0].length > 0 && paragraphs[1] === "" && paragraphs[2] === ""; | |||
|
|||
// Check for alternative Heading Two syntax | |||
for (let i = 0; i < paragraphs.length; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can eliminate checking for condition i < paragraphs.length - 1
by limiting the iteration condition.
Example:
for (let i = 0; i < (paragraphs.length - 1); i++) {
if (i < paragraphs[i + 1].match(/^( {0,3}-+\s*)$/) !== null) {
paragraphs[i] = `${paragraphs[i]}\n${paragraphs[i + 1]}`;
paragraphs[i + 1] = "";
}
}
src/parse-markdown/index.ts
Outdated
@@ -9,6 +9,14 @@ export default function parseMarkdown(md: string, fname: string) { | |||
const hasTitle = | |||
paragraphs[0].length > 0 && paragraphs[1] === "" && paragraphs[2] === ""; | |||
|
|||
// Check for alternative Heading Two syntax | |||
for (let i = 0; i < paragraphs.length; i++) { | |||
if (i < paragraphs.length - 1 && paragraphs[i + 1].match(/^( {0,3}-+\s*)$/) != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strict type checking operators are preferred (!==
)
I have completed making the necessary changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Thanks
Resolves #9
parseArguments.ts
to also check if input path ends in.md
parseHeadingTwo.ts
file inside./src/parse-markdown
folder and added function to replace Heading 2 markdown syntax with appropriate<h2>...</h2>
HTML tag./src/parse-markdown/index.ts
, the input text is being split into singular lines, but since the alternative Heading 2 syntax is based on two lines (alternate syntax is having a form of---
on the line after the desired Heading 2 line), I needed to join back the lines that matched this type of syntax, and then push it to theparseBlock(paragraph)
functionparseBlock.ts
to runparseHeadingTwo
function withinparseBlock
function.md
sample files inside ./examples folder