Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ $ ts-node src/index.ts -input examples --stylesheet https://cdn.jsdelivr.net/npm
</html>
```
```html

./til/text2.html

<!doctype html>
Expand All @@ -328,6 +329,38 @@ $ ts-node src/index.ts -input examples --stylesheet https://cdn.jsdelivr.net/npm
</html>
```

#### Example to convert Markdown file to HTML file

```bash
ts-node src/index.ts -i examples/text3.md
```

```bash
./til/text3.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Format</title>
</head>
<body>
<p>This is a paragraph of text in <strong>HTML</strong>.</p>
<p><em>This is italic text.</em></p>
<p><strong>This is bold text.</strong></p>
<p><em><strong>This is italic and bold text.</strong></em></p>
</body>
</html>

```

#### 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)
7 changes: 7 additions & 0 deletions example/t.md
Original file line number Diff line number Diff line change
@@ -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.***
14 changes: 14 additions & 0 deletions examples/text3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Format</title>
</head>
<body>
<p>This is a paragraph of text in <strong>HTML</strong>.</p>
<p><em>This is italic text.</em></p>
<p><strong>This is bold text.</strong></p>
<p><em><strong>This is italic and bold text.</strong></em></p>
</body>
</html>
7 changes: 7 additions & 0 deletions examples/text3.md
Original file line number Diff line number Diff line change
@@ -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.***
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`));
Expand Down
58 changes: 58 additions & 0 deletions src/utility/writeFile.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
export function htmlCreator(
line: string[],
title: string,
Expand Down Expand Up @@ -26,3 +27,60 @@ export function htmlCreator(
</html>`;
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</$1>');

// 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 = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>${title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${
stylesheetURL === ""
? ""
: `<link rel="stylesheet" href="${stylesheetURL}">`
}
</head>
<body>
${content}
</body>
</html>`;

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}`);
}
}