From f1a565106da7c81ff0cf6c93bce465c6b62796d4 Mon Sep 17 00:00:00 2001 From: shshah135 Date: Fri, 22 Sep 2023 12:25:19 -0400 Subject: [PATCH 1/5] taking markdown files --- README.md | 32 ++++++++++++++++++++++ example/t.md | 8 ++++++ examples/text3.html | 14 ++++++++++ examples/text3.md | 8 ++++++ src/index.ts | 10 +++++++ src/utility/writeFile.ts | 57 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 129 insertions(+) create mode 100644 example/t.md create mode 100644 examples/text3.html create mode 100644 examples/text3.md diff --git a/README.md b/README.md index 1005040..8fce5a3 100644 --- a/README.md +++ b/README.md @@ -328,6 +328,38 @@ $ ts-node src/index.ts -input examples --stylesheet https://cdn.jsdelivr.net/npm ``` +#### Example to convert Markdown file to HTML file + +```bash +ts-node src/index.ts -i examples/text3.md +``` + +```bash +./til/text3.md + + + + + + + HTML Format + + +

This is a paragraph of text in HTML.

+

This is italic text.

+

This is bold text.

+

This is italic and bold text.

+ + + +``` + +#### To convert Markdown Directory to html file + +```bash +ts-node src/index.ts -i example -o til +``` + ## License [MIT](https://github.com/seog-jun/til-tool/blob/main/LICENSE) diff --git a/example/t.md b/example/t.md new file mode 100644 index 0000000..37b58a7 --- /dev/null +++ b/example/t.md @@ -0,0 +1,8 @@ + +This is a paragraph of text in **Markdown**. + +*This is italic text.* + +**This is bold text.** + +***This is italic and bold text.*** diff --git a/examples/text3.html b/examples/text3.html new file mode 100644 index 0000000..24c24ac --- /dev/null +++ b/examples/text3.html @@ -0,0 +1,14 @@ + + + + + + HTML Format + + +

This is a paragraph of text in HTML.

+

This is italic text.

+

This is bold text.

+

This is italic and bold text.

+ + diff --git a/examples/text3.md b/examples/text3.md new file mode 100644 index 0000000..37b58a7 --- /dev/null +++ b/examples/text3.md @@ -0,0 +1,8 @@ + +This is a paragraph of text in **Markdown**. + +*This is italic text.* + +**This is bold text.** + +***This is italic and bold text.*** diff --git a/src/index.ts b/src/index.ts index dcb158d..c786f64 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,16 @@ const inputValue = program.opts().input; const outputValue = program.opts().output; const styleValue = program.opts().stylesheet; +if (!inputValue) { + console.error("Input file or directory is required."); + process.exit(1); // Exit with an error code +} + +if (!fs.existsSync(inputValue)) { + console.error("Input file or directory does not exist."); + process.exit(1); // Exit with an error code +} + if (options.input) { fs.rmSync(path.join(__dirname, `../til`), { recursive: true, force: true }); fs.mkdirSync(path.join(__dirname, `../til`)); diff --git a/src/utility/writeFile.ts b/src/utility/writeFile.ts index df00f10..1936fd3 100644 --- a/src/utility/writeFile.ts +++ b/src/utility/writeFile.ts @@ -1,3 +1,4 @@ +import fs from 'fs'; export function htmlCreator( line: string[], title: string, @@ -26,3 +27,59 @@ export function htmlCreator( `; return htmlString; } + +// Function to convert Markdown to HTML +function markdownToHTML(markdownText: string): string { + // Replace italic and bold markdown with HTML tags + markdownText = markdownText.replace(/(\*{1,2})([^\*]+)\1/g, '<$1>$2'); + + // You can add more Markdown to HTML conversions here as needed + // For example, handling headings, links, etc. + + return markdownText; +} + +// Function to create HTML from content +function createHTMLContent(content: string, title: string, stylesheetURL: string = ""): string { + const htmlString = ` + + + + ${title} + + ${ + stylesheetURL === "" + ? "" + : `` + } + + + ${content} + + `; + + return htmlString; +} + +// Function to create HTML from a file (supporting both .txt and .md) +export function htmlCreatorFromFile( + filePath: string, + title: string, + stylesheetURL: string = "" +): string { + const fileExtension = filePath.slice(((filePath.lastIndexOf(".") - 1) >>> 0) + 2); + + if (fileExtension === 'txt') { + // Read and process .txt file + const fileContent = fs.readFileSync(filePath, 'utf-8'); + return createHTMLContent(fileContent, title, stylesheetURL); + } else if (fileExtension === 'md') { + // Read and process .md file with Markdown to HTML conversion + const fileContent = fs.readFileSync(filePath, 'utf-8'); + const htmlContent = markdownToHTML(fileContent); + + return createHTMLContent(htmlContent, title, stylesheetURL); + } else { + throw new Error(`Unsupported file extension: .${fileExtension}`); + } +} From 6736fdf35f4a2a626c626f8edf279affd9e18b45 Mon Sep 17 00:00:00 2001 From: shshah135 Date: Fri, 22 Sep 2023 12:38:44 -0400 Subject: [PATCH 2/5] Issue #7: Adding Markdown support to the tool --- README.md | 2 +- examples/text3.html | 2 +- examples/text3.md | 2 +- src/index.ts | 2 ++ src/utility/writeFile.ts | 1 + 5 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8fce5a3..83a4ec6 100644 --- a/README.md +++ b/README.md @@ -335,7 +335,7 @@ ts-node src/index.ts -i examples/text3.md ``` ```bash -./til/text3.md +./til/text3.html diff --git a/examples/text3.html b/examples/text3.html index 24c24ac..8cd754f 100644 --- a/examples/text3.html +++ b/examples/text3.html @@ -3,7 +3,7 @@ - HTML Format + Markdown File

This is a paragraph of text in HTML.

diff --git a/examples/text3.md b/examples/text3.md index 37b58a7..c955f52 100644 --- a/examples/text3.md +++ b/examples/text3.md @@ -1,5 +1,5 @@ -This is a paragraph of text in **Markdown**. +This is a paragraph of text is in **Markdown**. *This is italic text.* diff --git a/src/index.ts b/src/index.ts index c786f64..fef4f25 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,8 @@ const inputValue = program.opts().input; const outputValue = program.opts().output; const styleValue = program.opts().stylesheet; +// to differentiate between file and directory + if (!inputValue) { console.error("Input file or directory is required."); process.exit(1); // Exit with an error code diff --git a/src/utility/writeFile.ts b/src/utility/writeFile.ts index 1936fd3..c4df112 100644 --- a/src/utility/writeFile.ts +++ b/src/utility/writeFile.ts @@ -62,6 +62,7 @@ function createHTMLContent(content: string, title: string, stylesheetURL: string } // Function to create HTML from a file (supporting both .txt and .md) + export function htmlCreatorFromFile( filePath: string, title: string, From d23592d58fd91ffeee1b4e7cc85d4fcebf4b420b Mon Sep 17 00:00:00 2001 From: shshah135 Date: Fri, 22 Sep 2023 12:54:37 -0400 Subject: [PATCH 3/5] Issue #7 : Adding Markdown support to the tool --- examples/text3.html | 2 +- examples/text3.md | 2 +- src/index.ts | 2 +- src/utility/writeFile.ts | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/text3.html b/examples/text3.html index 8cd754f..24c24ac 100644 --- a/examples/text3.html +++ b/examples/text3.html @@ -3,7 +3,7 @@ - Markdown File + HTML Format

This is a paragraph of text in HTML.

diff --git a/examples/text3.md b/examples/text3.md index c955f52..db61909 100644 --- a/examples/text3.md +++ b/examples/text3.md @@ -5,4 +5,4 @@ This is a paragraph of text is in **Markdown**. **This is bold text.** -***This is italic and bold text.*** +***This is italic and bold text*** diff --git a/src/index.ts b/src/index.ts index fef4f25..c015f7d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,7 +27,7 @@ const inputValue = program.opts().input; const outputValue = program.opts().output; const styleValue = program.opts().stylesheet; -// to differentiate between file and directory +// to differentiate between file and direc if (!inputValue) { console.error("Input file or directory is required."); diff --git a/src/utility/writeFile.ts b/src/utility/writeFile.ts index c4df112..02a172a 100644 --- a/src/utility/writeFile.ts +++ b/src/utility/writeFile.ts @@ -34,7 +34,6 @@ function markdownToHTML(markdownText: string): string { markdownText = markdownText.replace(/(\*{1,2})([^\*]+)\1/g, '<$1>$2'); // You can add more Markdown to HTML conversions here as needed - // For example, handling headings, links, etc. return markdownText; } From a706c0c8e4f95109335dfa43b32cb351891ba325 Mon Sep 17 00:00:00 2001 From: shshah135 Date: Fri, 22 Sep 2023 13:11:07 -0400 Subject: [PATCH 4/5] Implementing Markdown support --- README.md | 1 + example/t.md | 1 - examples/text3.html | 2 +- examples/text3.md | 1 - src/index.ts | 2 +- src/utility/writeFile.ts | 2 +- 6 files changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 83a4ec6..90c5ca4 100644 --- a/README.md +++ b/README.md @@ -309,6 +309,7 @@ $ ts-node src/index.ts -input examples --stylesheet https://cdn.jsdelivr.net/npm ``` ```html + ./til/text2.html diff --git a/example/t.md b/example/t.md index 37b58a7..68f4b1b 100644 --- a/example/t.md +++ b/example/t.md @@ -1,4 +1,3 @@ - This is a paragraph of text in **Markdown**. *This is italic text.* diff --git a/examples/text3.html b/examples/text3.html index 24c24ac..67b89ed 100644 --- a/examples/text3.html +++ b/examples/text3.html @@ -3,7 +3,7 @@ - HTML Format + Html Format

This is a paragraph of text in HTML.

diff --git a/examples/text3.md b/examples/text3.md index db61909..dbbd980 100644 --- a/examples/text3.md +++ b/examples/text3.md @@ -1,4 +1,3 @@ - This is a paragraph of text is in **Markdown**. *This is italic text.* diff --git a/src/index.ts b/src/index.ts index c015f7d..fef4f25 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,7 +27,7 @@ const inputValue = program.opts().input; const outputValue = program.opts().output; const styleValue = program.opts().stylesheet; -// to differentiate between file and direc +// to differentiate between file and directory if (!inputValue) { console.error("Input file or directory is required."); diff --git a/src/utility/writeFile.ts b/src/utility/writeFile.ts index 02a172a..209b6b5 100644 --- a/src/utility/writeFile.ts +++ b/src/utility/writeFile.ts @@ -60,7 +60,7 @@ function createHTMLContent(content: string, title: string, stylesheetURL: string return htmlString; } -// Function to create HTML from a file (supporting both .txt and .md) +// Function to create HTML from a file (supporting both .md and .txt) export function htmlCreatorFromFile( filePath: string, From ce312f464fdc272df20e76cf0ccfc4d4caebdf1b Mon Sep 17 00:00:00 2001 From: shshah135 Date: Fri, 22 Sep 2023 13:40:25 -0400 Subject: [PATCH 5/5] Implementing to convert .md to .html --- examples/text3.html | 2 +- examples/text3.md | 2 +- src/index.ts | 1 + src/utility/writeFile.ts | 3 ++- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/text3.html b/examples/text3.html index 67b89ed..24c24ac 100644 --- a/examples/text3.html +++ b/examples/text3.html @@ -3,7 +3,7 @@ - Html Format + HTML Format

This is a paragraph of text in HTML.

diff --git a/examples/text3.md b/examples/text3.md index dbbd980..b243d52 100644 --- a/examples/text3.md +++ b/examples/text3.md @@ -4,4 +4,4 @@ This is a paragraph of text is in **Markdown**. **This is bold text.** -***This is italic and bold text*** +***This is italic and bold text.*** diff --git a/src/index.ts b/src/index.ts index fef4f25..7a22f22 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,7 @@ const inputValue = program.opts().input; const outputValue = program.opts().output; const styleValue = program.opts().stylesheet; +//to support both file and directory for input - md file // to differentiate between file and directory if (!inputValue) { diff --git a/src/utility/writeFile.ts b/src/utility/writeFile.ts index 209b6b5..d0b62f7 100644 --- a/src/utility/writeFile.ts +++ b/src/utility/writeFile.ts @@ -27,7 +27,7 @@ export function htmlCreator( `; return htmlString; } - +// lab 2 // Function to convert Markdown to HTML function markdownToHTML(markdownText: string): string { // Replace italic and bold markdown with HTML tags @@ -83,3 +83,4 @@ export function htmlCreatorFromFile( throw new Error(`Unsupported file extension: .${fileExtension}`); } } +