Skip to content

Commit c957006

Browse files
author
tjk
committed
Update readme to reflect API changes
1 parent e1a3746 commit c957006

File tree

1 file changed

+76
-73
lines changed

1 file changed

+76
-73
lines changed

lib/README.md

Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
# pulldown-html-ext
22

3-
A flexible Markdown to HTML renderer built on top of pulldown-cmark with support for custom styling, attributes, and rendering options.
3+
A configurable Markdown to HTML renderer that extends [pulldown-cmark](https://github.com/raphlinus/pulldown-cmark). This library provides a flexible HTML rendering system with extensive configuration options, custom styling support, and attribute handling capabilities.
44

55
## Features
66

7-
- Configurable HTML rendering with support for custom attributes and classes
8-
- Extensive options for headings, links, code blocks, and other elements
9-
- Support for external link handling (nofollow, target="_blank")
10-
- Custom ID generation for headings
7+
- Configurable HTML rendering with extensive options
8+
- Custom attribute mapping for HTML elements
9+
- Support for heading IDs and custom classes
10+
- Customizable code block rendering
11+
- External link handling with `nofollow` and `target="_blank"` options
12+
- Table support with alignment controls
13+
- Footnote rendering
14+
- Task list support
1115
- XHTML-style output option
12-
- Table support with alignment
13-
- Footnotes and task lists
14-
- Line number support for code blocks
16+
- Pretty printing support
1517

16-
## Usage
18+
## Installation
1719

1820
Add this to your `Cargo.toml`:
1921

@@ -22,89 +24,95 @@ Add this to your `Cargo.toml`:
2224
pulldown-html-ext = "0.1.0"
2325
```
2426

25-
### Basic Usage
27+
## Quick Start
2628

27-
For basic markdown rendering with default options (this should yield the same output as `pulldown-cmark` rendering):
29+
Here's a simple example of converting Markdown to HTML using default settings:
2830

2931
```rust
3032
use pulldown_html_ext::{HtmlConfig, push_html};
3133

32-
fn main() {
33-
let config = HtmlConfig::default();
34-
let markdown = "# Hello\nThis is *markdown*";
35-
let html = push_html(markdown, &config);
36-
println!("{}", html);
37-
}
34+
let config = HtmlConfig::default();
35+
let markdown = "# Hello\nThis is *markdown*";
36+
let html = push_html(markdown, &config);
3837
```
3938

40-
### Custom Configuration
39+
## Configuration
4140

42-
You can customize the rendering behavior:
41+
The library provides extensive configuration options through the `HtmlConfig` struct:
4342

4443
```rust
45-
use pulldown_html_ext::{HtmlConfig, push_html};
46-
use std::collections::HashMap;
44+
let mut config = HtmlConfig::default();
4745

48-
fn main() {
49-
let mut config = HtmlConfig::default();
50-
51-
// Configure heading classes
52-
config.elements.headings.level_classes = {
53-
let mut map = HashMap::new();
54-
map.insert(1, "title".to_string());
55-
map.insert(2, "subtitle".to_string());
56-
map
57-
};
58-
46+
// Configure HTML options
47+
config.html.escape_html = true;
48+
config.html.break_on_newline = true;
49+
config.html.xhtml_style = false;
50+
config.html.pretty_print = true;
5951

60-
// Configure external links
61-
config.elements.links.nofollow_external = true;
62-
config.elements.links.open_external_blank = true;
63-
64-
// Configure HTML output
65-
config.html.escape_html = true;
66-
config.html.break_on_newline = false;
67-
config.html.xhtml_style = true;
68-
69-
let markdown = r#"
70-
# Main Title
71-
## Subtitle
52+
// Configure heading options
53+
config.elements.headings.add_ids = true;
54+
config.elements.headings.id_prefix = "heading-".to_string();
7255

73-
[External Link](https://example.com)
74-
"#;
75-
76-
let html = push_html(markdown, &config);
77-
println!("{}", html);
78-
}
56+
// Configure link options
57+
config.elements.links.nofollow_external = true;
58+
config.elements.links.open_external_blank = true;
59+
60+
// Configure code block options
61+
config.elements.code_blocks.default_language = Some("rust".to_string());
62+
config.elements.code_blocks.line_numbers = false;
7963
```
8064

81-
### Custom Rendering
65+
## Custom Attribute Mapping
8266

83-
For more control, you can implement your own HTML writer and override specific rendering methods:
67+
You can add custom attributes to HTML elements:
68+
69+
```rust
70+
use std::collections::HashMap;
71+
72+
let mut config = HtmlConfig::default();
73+
let mut attrs = HashMap::new();
74+
attrs.insert("class".to_string(), "custom-paragraph".to_string());
75+
config.attributes.element_attributes.insert("p".to_string(), attrs);
76+
```
77+
78+
## Custom Writers
79+
80+
Create custom HTML writers by implementing the `HtmlWriter` trait. This allows you to customize how specific Markdown elements are rendered to HTML:
8481

8582
```rust
8683
use pulldown_html_ext::{HtmlConfig, HtmlWriter, HtmlState, create_html_renderer};
84+
use pulldown_cmark_escape::{StrWrite, FmtWriter};
8785
use pulldown_cmark::{HeadingLevel, Parser};
8886

89-
struct CustomWriter {
87+
struct CustomWriter<W: StrWrite> {
88+
writer: W,
9089
config: HtmlConfig,
91-
output: String,
9290
state: HtmlState,
9391
}
9492

95-
impl HtmlWriter for CustomWriter {
93+
impl<W: StrWrite> CustomWriter<W> {
94+
fn new(writer: W, config: HtmlConfig) -> Self {
95+
Self {
96+
writer,
97+
config,
98+
state: HtmlState::new(),
99+
}
100+
}
101+
}
102+
103+
impl<W: StrWrite> HtmlWriter<W> for CustomWriter<W> {
104+
fn get_writer(&mut self) -> &mut W {
105+
&mut self.writer
106+
}
107+
96108
fn get_config(&self) -> &HtmlConfig {
97109
&self.config
98110
}
99-
100-
fn get_output(&mut self) -> &mut String {
101-
&mut self.output
102-
}
103-
111+
104112
fn get_state(&mut self) -> &mut HtmlState {
105113
&mut self.state
106114
}
107-
115+
108116
// Override heading rendering to add emoji markers and custom classes
109117
fn start_heading(&mut self, level: HeadingLevel, _id: Option<&str>, classes: Vec<&str>) {
110118
let level_num = self.heading_level_to_u8(level);
@@ -130,32 +138,27 @@ impl HtmlWriter for CustomWriter {
130138
}
131139
}
132140

141+
// Usage example:
133142
fn main() {
134-
let writer = CustomWriter {
135-
config: HtmlConfig::default(),
136-
output: String::new(),
137-
state: HtmlState::new(),
138-
};
143+
let mut output = String::new();
144+
let writer = CustomWriter::new(FmtWriter(&mut output), HtmlConfig::default());
139145
let mut renderer = create_html_renderer(writer);
140146

141147
let markdown = "# Main Title\n## Subtitle\n### Section";
142148
let parser = Parser::new(markdown);
143149
renderer.run(parser);
144150

145-
// This will output:
151+
println!("{}", output);
152+
// Output:
146153
// <h1 class="fancy-heading level-1">🎯 Main Title </h1>
147154
// <h2 class="fancy-heading level-2">💫 Subtitle </h2>
148155
// <h3 class="fancy-heading level-3">✨ Section </h3>
149-
println!("{}", renderer.writer.output);
150156
}
151157
```
152158

159+
## Contributing
153160

154-
You can override any of the methods from the `HtmlWriter` trait to customize the rendering of different Markdown elements. Some commonly overridden methods include:
155-
- `start_link`/`end_link` for custom link rendering
156-
- `start_code_block`/`end_code_block` for custom code block formatting
157-
- `start_list`/`end_list` for custom list rendering
158-
- `text` for custom text processing
161+
Contributions are welcome! Please feel free to submit a Pull Request.
159162

160163
## License
161164

0 commit comments

Comments
 (0)