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 #13

Merged
merged 10 commits into from Sep 21, 2023
Merged

Conversation

mismathh
Copy link
Contributor

Resolves #9

  • Updated parseArguments.ts to also check if input path ends in .md
  • Created parseHeadingTwo.ts file inside ./src/parse-markdown folder and added function to replace Heading 2 markdown syntax with appropriate <h2>...</h2> HTML tag
    • Within ./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 the parseBlock(paragraph) function
  • Updated parseBlock.ts to run parseHeadingTwo function within parseBlock function
  • Added .md sample files inside ./examples folder
  • Updated README to include file extensions that are supported

Copy link
Owner

@paulkim26 paulkim26 left a 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.

@@ -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}`;
Copy link
Owner

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();
Copy link
Owner

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();
Copy link
Owner

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.

@@ -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++) {
Copy link
Owner

@paulkim26 paulkim26 Sep 20, 2023

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] = "";
  }
}

@@ -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) {
Copy link
Owner

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 (!==)

@mismathh
Copy link
Contributor Author

I have completed making the necessary changes.

@mismathh mismathh closed this Sep 21, 2023
@mismathh mismathh reopened this Sep 21, 2023
Copy link
Owner

@paulkim26 paulkim26 left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks

@paulkim26 paulkim26 merged commit e41bbe0 into paulkim26:main Sep 21, 2023
@mismathh mismathh deleted the issue-9 branch September 21, 2023 00:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add markdown support (Lab#2)
2 participants