Skip to content

Commit c03c9da

Browse files
Fix Beehiiv markdown conversion spacing issues (#124)
### Motivation - Markdown parsed from VS Code was producing extra empty paragraph elements and trailing newlines in code blocks, which resulted in too many blank lines and structurally odd HTML when pasted into Beehiiv. ### Description - Added `isEmptyParagraph` to drop empty `<p>` nodes produced by the Markdown parser so blank paragraphs are not emitted. - Changed first-block detection to set `data-pm-slice` on the first emitted block after filtering using `blocks.length === 0` instead of the original node index logic. - Trimmed a trailing newline from fenced code block content with `code.textContent = code.textContent.replace(/\n$/, '');` to avoid extra blank lines inside `<pre><code>` output. - Emit lists as top-level `<ul>`/`<ol>` elements (creating `list` via `document.createElement(node.tagName.toLowerCase())`) instead of wrapping them inside a `<p>`, which prevented extra spacing between list items. ------ [Codex Task](https://chatgpt.com/codex/tasks/task_e_69c542e1766483259b2c3d0256f3447d)
1 parent 03e7eba commit c03c9da

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

vscode-to-beehiiv.html

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ <h2>Beehiiv output</h2>
138138
}
139139
};
140140

141+
const isEmptyParagraph = (element) => {
142+
if (element.tagName !== 'P') {
143+
return false;
144+
}
145+
146+
const hasMedia = element.querySelector('img, video, iframe, embed, object');
147+
const text = element.textContent.replace(/\u00A0/g, ' ').trim();
148+
return !hasMedia && text === '';
149+
};
150+
141151
const convertMarkdownToBeehiiv = (markdown) => {
142152
if (!markdown.trim()) {
143153
return '';
@@ -167,9 +177,13 @@ <h2>Beehiiv output</h2>
167177
}
168178

169179
if (node.tagName === 'P') {
180+
if (isEmptyParagraph(node)) {
181+
return;
182+
}
183+
170184
const paragraph = document.createElement('p');
171185
paragraph.setAttribute('data-id', crypto.randomUUID());
172-
if (index === 0) {
186+
if (blocks.length === 0) {
173187
paragraph.setAttribute('data-pm-slice', '1 1 []');
174188
}
175189
paragraph.setAttribute('style', BLOCK_STYLE);
@@ -197,18 +211,19 @@ <h2>Beehiiv output</h2>
197211

198212
pre.querySelectorAll('code').forEach(code => {
199213
normalizeCodeLanguage(code);
214+
code.textContent = code.textContent.replace(/\n$/, '');
200215
});
201216

202217
blocks.push(pre.outerHTML);
203218
return;
204219
}
205220

206221
if (node.tagName === 'UL' || node.tagName === 'OL') {
207-
const listParagraph = document.createElement('p');
208-
listParagraph.setAttribute('data-id', crypto.randomUUID());
209-
listParagraph.setAttribute('style', BLOCK_STYLE);
210-
listParagraph.innerHTML = node.outerHTML;
211-
blocks.push(listParagraph.outerHTML);
222+
const list = document.createElement(node.tagName.toLowerCase());
223+
list.setAttribute('data-id', crypto.randomUUID());
224+
list.setAttribute('style', BLOCK_STYLE);
225+
list.innerHTML = node.innerHTML;
226+
blocks.push(list.outerHTML);
212227
return;
213228
}
214229

0 commit comments

Comments
 (0)