diff --git a/README.md b/README.md index 1005040..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 @@ -328,6 +329,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.html + + + + + + + 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..68f4b1b --- /dev/null +++ b/example/t.md @@ -0,0 +1,7 @@ +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..b243d52 --- /dev/null +++ b/examples/text3.md @@ -0,0 +1,7 @@ +This is a paragraph of text is 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..7a22f22 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,19 @@ 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) { + 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..d0b62f7 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,60 @@ 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 + markdownText = markdownText.replace(/(\*{1,2})([^\*]+)\1/g, '<$1>$2'); + + // You can add more Markdown to HTML conversions here as needed + + 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 .md and .txt) + +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}`); + } +} +