Skip to content

Commit 8c5aa49

Browse files
committed
docs: add comprehensive newline handling documentation
Addresses #69 and #66 by documenting expected behavior: - Explains paragraph spacing is standard markdown (blank lines between paragraphs) - Documents line breaks vs paragraphs behavior - Provides clear examples of maxConsecutiveNewlines option usage - Shows how to control consecutive newlines for different use cases Both issues are by-design behavior, not bugs. The maxConsecutiveNewlines option (default: 3) already provides the control users need.
1 parent 10e5c2a commit 8c5aa49

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,85 @@ export interface NodeHtmlMarkdownOptions {
229229
}
230230
```
231231

232+
## Newline Handling
233+
234+
### Understanding Paragraph Spacing
235+
236+
In standard Markdown, paragraphs are separated by blank lines. This library follows this convention, so HTML block elements like `<p>`, `<div>`, `<h1>`, etc. are surrounded by blank lines in the output.
237+
238+
**Example:**
239+
240+
```ts
241+
const html = `<p>Hello</p><p>World</p><p>!</p>`;
242+
const markdown = NodeHtmlMarkdown.translate(html);
243+
console.log(markdown);
244+
// Output:
245+
// Hello
246+
//
247+
// World
248+
//
249+
// !
250+
```
251+
252+
This is the expected behavior and produces valid, readable Markdown. If you need tighter spacing, consider using line breaks instead of paragraphs.
253+
254+
### Line Breaks vs Paragraphs
255+
256+
- **Paragraphs** (`<p>`) create blank lines between content (standard Markdown behavior)
257+
- **Line breaks** (`<br>`) create single line breaks with two trailing spaces (Markdown line break syntax)
258+
259+
**Example:**
260+
261+
```ts
262+
// Using line breaks
263+
const html = `<p>Line 1<br>Line 2<br>Line 3</p>`;
264+
const markdown = NodeHtmlMarkdown.translate(html);
265+
console.log(markdown);
266+
// Output:
267+
// Line 1
268+
// Line 2
269+
// Line 3
270+
```
271+
272+
### Controlling Consecutive Newlines
273+
274+
The `maxConsecutiveNewlines` option (default: `3`) limits how many consecutive newlines appear in the output. This helps keep the Markdown clean and prevents excessive whitespace.
275+
276+
**Example with multiple `<br>` tags:**
277+
278+
```ts
279+
// Default behavior - limits to 3 consecutive newlines
280+
const html = `<p>a</p>${'<br>'.repeat(10)}<p>b</p>`;
281+
const markdown = NodeHtmlMarkdown.translate(html);
282+
// Result has maximum 3 consecutive line breaks
283+
284+
// Allow more consecutive newlines
285+
const markdown2 = NodeHtmlMarkdown.translate(html, {
286+
maxConsecutiveNewlines: 10
287+
});
288+
// Result preserves all 10 line breaks
289+
```
290+
291+
**Example with inline elements:**
292+
293+
```ts
294+
const html = `<b>text</b>${'<br>'.repeat(10)}<em>something</em>`;
295+
296+
// Default (max 3 newlines)
297+
NodeHtmlMarkdown.translate(html);
298+
// Output: **text** \n \n \n_something_
299+
300+
// Custom (max 10 newlines)
301+
NodeHtmlMarkdown.translate(html, { maxConsecutiveNewlines: 10 });
302+
// Output: **text** \n \n \n \n \n \n \n \n \n \n_something_
303+
```
304+
305+
**When to adjust this setting:**
306+
307+
- **Decrease** (e.g., `maxConsecutiveNewlines: 1`) for more compact output
308+
- **Increase** (e.g., `maxConsecutiveNewlines: 10`) when you need to preserve spacing from the source HTML
309+
- **Keep default** (`3`) for balanced, readable Markdown output
310+
232311
## Custom Translators
233312

234313
Custom translators are an advanced option to allow handling certain elements a specific way.

0 commit comments

Comments
 (0)