-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 8 commits
de6b59c
2c6f43b
cef5cec
0dccdc4
46bad11
c5d1ad0
214abe6
410fb21
c1f31f3
c37e084
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
This is a Heading 2 markdown test file 1 | ||
|
||
|
||
First Heading 2 ## Testing out | ||
-- | ||
A TIL—Today I Learned—is the most liberating form of content I know of. | ||
|
||
Did you just learn how to do something? Write about that. | ||
|
||
Call it a TIL—that way you’re not promising anyone a revelation or an in-depth tutorial. You’re saying “I just figured this out: here are my notes, you may find them useful too”. | ||
|
||
I also like the humility of this kind of content. Part of the reason I publish them is to emphasize that even with 25 years of professional experience you should still celebrate learning even the most basic of things. | ||
|
||
Second Heading 2 | ||
----------------------------------------- | ||
I learned the “interact” command in pdb the other day! [Here’s my TIL](https://til.simonwillison.net/python/pdb-interact). | ||
|
||
I started publishing TILs [in April 2020](https://simonwillison.net/2020/Apr/20/self-rewriting-readme/). I’m up to 346 now, and most of them took less than 10 minutes to write. It’s such a great format for quick and satisfying online writing. | ||
|
||
My collection lives at https://til.simonwillison.net/—which publishes content from my [simonw/til](https://github.com/simonw/til) GitHub repository. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
markdown-to-html test | ||
|
||
|
||
A tool to convert [TIL](https://simonwillison.net/2022/Nov/6/what-to-blog-about/) posts written in [Markdown](https://www.markdownguide.org/) into **static HTML pages**. | ||
|
||
## Usage for Flags - Heading 2 | ||
Run **til-to-html -h** for additional help. | ||
|
||
Run **til-to-html -v** for version information. | ||
|
||
## Usage for Paths - Heading 2 | ||
Run **til-to-html ./examples/til.txt** to generate a single webpage. | ||
|
||
Run **til-to-html ./examples/dir** to generate multiple webpages from a folder. | ||
|
||
Refer to the [til-to-html repository](https://github.com/paulkim26/til-to-html) for additional information. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Strict type checking operators are preferred ( |
||
paragraphs[i] = `${paragraphs[i]}\n${paragraphs[i + 1]}`; | ||
paragraphs[i + 1] = ""; | ||
} | ||
} | ||
|
||
// Clear empty paragraphs | ||
paragraphs = paragraphs.filter((paragraph) => paragraph !== ""); | ||
|
||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. No need for template syntax, can simply do: lineHtml += parsedParagraph; |
||
} else { | ||
lineHtml += `<p>${parsedParagraph}</p>`; | ||
} | ||
} | ||
|
||
bodyHtml += `${lineHtml}\n`; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export default function parseHeadingTwo(text: string) { | ||
let html = text; | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. I prefer using |
||
return `<h2>${headingTwo}</h2>`; | ||
}); | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. I prefer using |
||
return `<h2>${altHeadingTwo}</h2>`; | ||
}); | ||
|
||
return html; | ||
} |
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: