Introduction
Markdown converters are small compilers: a block-level state machine decides what each line is, and an inline pass turns **bold** and links into tags. Getting the details right — escaping HTML, protecting code spans, rejecting javascript: links — is where the real learning is.
Problem statement
Objectives
- Build a block-level state machine
- Apply ordered regular-expression transformations safely
- Protect code spans with placeholders
- Sanitise user-provided URLs
Features
- Headings with de-duplicated ids and a TOC
- Lists, task lists, quotes, rules and code blocks
- Inline formatting with correct nesting order
- HTML escaping and javascript: link blocking
Concepts demonstrated
- Parsing
- State machines
- Escaping and sanitisation
- Regular expressions
- Placeholders
System architecture
MarkdownConverter keeps the currently open block (NONE, PARAGRAPH, UL, OL, QUOTE or CODE). Each line either continues that block or closes it and opens another; close() emits the closing tag. Headings get ids by lower-casing and replacing non-alphanumerics with hyphens, with -2, -3 suffixes for repeats. Inline.render first swaps code spans for placeholders, escapes the rest, applies images before links (so  isn't treated as a link), then bold, italic and strikethrough, and finally restores the code spans.
Folder structure
Complete source code
src/Inline.java
What each file does
- src/Inline.java
- Escaping, safe URLs and the inline rules (code, images, links, bold, italic, strikethrough).
- src/MarkdownConverter.java
- The block state machine, heading ids and the table of contents.
- src/Main.java
- Reads standard input and prints the HTML and statistics.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
# Java Notes
Java is **statically typed** and *object-oriented*.
It runs on the `JVM` & is ~~slow~~ fast.
## Getting Started
1. Install a JDK
2. Write `Main.java`
3. Run `java Main.java`
- Use **records** for data
- Read the [official docs](https://docs.oracle.com/en/java/)
* Never click [this](javascript:stealCookies)
## Example
```java
if (a < b && list.size() > 0) {
System.out.println("<done>");
}
```
> Tip: `var` is not dynamic typing.
> It infers a **static** type.
---
## Example
- [x] Learn classes
- [ ] Learn streams
 Paragraph after an image with 2 * 3 = 6 and a_b_c.Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. &and<are escaped everywhere; inside the fenced Java block nothing is formatted.- The
javascript:link is rewritten to#. - The second
## Exampleheading gets the idexample-2.
Common errors
Underscores in a_b_c become italics
Require word boundaries around emphasis markers, as the ITALIC pattern does with look-arounds.
Code examples show <strong> tags
Extract code spans before applying inline rules and restore them afterwards.
Possible improvements
- Nested lists by indentation
- Tables in the GitHub Markdown style
- Wrap the output in a full HTML page with CSS
Additional challenges
- 1Add reference-style links [text][id].
- 2Support headings written with === and --- underlines.
- 3Write unit tests for each rule with JUnit.
Built and tested it yourself? Mark it to update your dashboard.