Skip to content

Commit 414393d

Browse files
Chore: fix markdown dedent function (#23)
Co-authored-by: Nate Moore <[email protected]>
1 parent 9a26d9e commit 414393d

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

.changeset/silent-insects-explode.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro-remote": patch
3+
---
4+
5+
Fixes an issue with internal `dedent` function used for spacing/linebreak corrections for `Markdown`

packages/astro-remote/lib/utils.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ function getIndent(ln: string): string {
4444
}
4545

4646
export function dedent(str: string): string {
47-
const lns = str.replace(/^[\r\n]+/, "").split("\n");
48-
let indent = getIndent(lns[0]);
49-
if (indent.length === 0 && lns.length > 1) {
50-
indent = getIndent(lns[1]);
51-
}
52-
return lns
53-
.map((ln) => (ln.startsWith(indent) ? ln.slice(indent.length) : ln))
54-
.map((ln, i, { length }) => (i === length - 1 ? ln.trim() : ln))
55-
.join("\n");
47+
const lns = str.replace(/^[\r\n]+/, "").split("\n");
48+
let indent = getIndent(lns[0]);
49+
if (indent.length === 0 && lns.length > 1) {
50+
indent = getIndent(lns[1]);
51+
}
52+
if (indent.length === 0) return lns.join("\n");
53+
return lns
54+
.map(ln => ln.startsWith(indent) ? ln.slice(indent.length) : ln)
55+
.join("\n");
5656
}
5757

5858
export interface HTMLOptions {

packages/playground/src/pages/index.astro

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<h2>Local:</h2>
1414
<ul>
1515
<li><a href="/test-md-local">Test Local Markdown "< Markdown >"</a></li>
16+
<li><a href="/test-issue7">Test Issue #7</a></li>
1617
</ul>
1718
<h2>Remote:</h2>
1819
<ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
import { Markdown } from "astro-remote";
3+
4+
const c0 = "Most .";
5+
const c1 = "Most \n.";
6+
const c2 = "Most \n.";
7+
const c3 = "Most . ";
8+
9+
const content = [c0, c1, c2, c3];
10+
---
11+
<div>
12+
{content.map((c) => <Markdown>{c}</Markdown>)}
13+
</div>

packages/playground/src/pages/test-md-local.astro

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const readme = `
1010
# Hello \`world\`
1111
1212
> [!NOTE]
13+
> This is a Note Using MarkedAlert Marked Extension\\
1314
> This is just a test
1415
1516
> **Note**

0 commit comments

Comments
 (0)