Skip to content

Commit 3a75211

Browse files
author
YumeiWang
committed
add feature: syntax highlighting for code blocks
1 parent e17a6fb commit 3a75211

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ConvertTxtMdToHtml
2-
A command-line tool can process input .txt files into generated .html files.
2+
A command-line tool can process input .txt or .md files into generated .html files.
33

44
## Installation
55
1. Download and Install Java Development Kit according to the Oracle [JDK Installation Guide](https://docs.oracle.com/en/java/javase/20/install/overview-jdk-installation.html#GUID-8677A77F-231A-40F7-98B9-1FD0B48C346A).
@@ -44,6 +44,7 @@ By default, if no output directory is specified, the `.html` files will be saved
4444
* Automatically parse title from `.txt` / `.md` file.
4545
* Converts .md syntax links to .html syntax links.
4646
* Converts .md `---` to `<hr>`.
47+
* Parse code block and can show line number by adding "showLineNumber" after first triple backticks
4748

4849
## Examples
4950
`java src/ConvertTxtMdToHtml.java ./examples`

convertTxtMdToHtml/testMD.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ <h1>Example TIL Post</h1>
1919
<p>When I found a bug, I went to the Issues tab of their GitHub project and clicked New issue.</p>
2020
<p></p>
2121
<p>I gave the issue a title and short description as well as a screenshot and submitted the issue.</p>
22+
<p></p>
23+
<style>
24+
pre {
25+
counter-reset: line;
26+
background-color: #f4f4f4;
27+
}
28+
code::before {
29+
content: counter(line);
30+
counter-increment: line;
31+
padding-right: 10px;
32+
display: inline-block;
33+
text-align: right;
34+
</style>
35+
<pre><code>const message = 'Hello, world!';</code>
36+
<code>console.log(message);</code>
37+
</pre>
38+
<p></p>
2239
<p><hr></p>
2340
<p>Have you heard about <a href="https://hacktoberfest.com/">Hacktoberfest</a>?</p>
2441
<p></p>

examples/testMD.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ Once I was familiar with how to run the program, I tried to run some edge cases
1313
When I found a bug, I went to the Issues tab of their GitHub project and clicked New issue.
1414

1515
I gave the issue a title and short description as well as a screenshot and submitted the issue.
16+
17+
```showLineNumber
18+
const message = 'Hello, world!';
19+
console.log(message);
20+
```
21+
1622
---
1723
Have you heard about [Hacktoberfest](https://hacktoberfest.com/)?
1824

19-
Testing --> [Yay][Hacktoberfest](https://hacktoberfest.com/)
25+
Testing --> [Yay][Hacktoberfest](https://hacktoberfest.com/)

src/ConvertTxtMdToHtml.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import java.util.regex.Matcher;
55
import java.util.regex.Pattern;
66

7-
87
public class ConvertTxtMdToHtml {
98
private static String DEFAULT_LANG = "en-CA"; // Default language is Canadian English
10-
private static String OUTPUT ="convertTxtMdToHtml";
9+
private static String OUTPUT = "convertTxtMdToHtml";
10+
1111
public static void main(String[] args) {
1212
// parse arguments
1313
if (args.length == 0 || args[0].equals("-h") || args[0].equals("--help")) {
@@ -117,13 +117,34 @@ private static void processFile(File inputFile, String outputPath, String lang)
117117
title = convertLinks(title); // convert links in MD file
118118
htmlContent.append("<h1>").append(title).append("</h1>\n");
119119
}
120+
121+
boolean isInsideCodeBlock = false;
122+
120123
for (String line : lines) {
121-
if (line.isEmpty()) {
124+
if (isInsideCodeBlock) {
125+
// If inside a code block, just add the line as-is
126+
if (line.trim().equals("```")) {
127+
isInsideCodeBlock = false; // Exit the code block
128+
htmlContent.append("</pre>\n");
129+
} else {
130+
htmlContent.append("<code>").append(line).append("</code>\n");
131+
}
132+
} else if (line.trim().equals("```")||line.trim().equals("```showLineNumber")) {
133+
// Start of a code block
134+
if( line.trim().equals("```showLineNumber")){
135+
htmlContent.append(
136+
"<style>\npre {\ncounter-reset: line;\nbackground-color: #f4f4f4;\n}\ncode::before {\ncontent: counter(line);\ncounter-increment: line;\npadding-right: 10px;\ndisplay: inline-block;\ntext-align: right;\n</style>\n<pre>");
137+
}else{
138+
htmlContent.append(
139+
"<style>\npre {\ncounter-reset: line;\nbackground-color: #f4f4f4;\n}\n</style>\n<pre>");
140+
}
141+
isInsideCodeBlock = true;
142+
} else if (line.isEmpty()) {
122143
htmlContent.append("<p></p>\n"); // Create a new paragraph
123144
} else {
124145
if (fileName.endsWith(".md"))
125146
line = convertHorizontal(line); // convert horizontal in MD file
126-
line = convertLinks(line); // convert links in MD file
147+
line = convertLinks(line); // convert links in MD file
127148
htmlContent.append("<p>").append(line).append("</p>\n");
128149
}
129150
}

0 commit comments

Comments
 (0)