Skip to content

Commit

Permalink
feat: support URL input for link renderer (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdikhashan authored Feb 12, 2024
1 parent f91ff9c commit 9a3d151
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ export function heading(text: string, level: number): string {
* @group render_utils
*/
export function link(
url: string,
url: string | URL,
text?: string,
opts?: { title?: string; external?: boolean },
): string {
if (opts?.external) {
return `<a href="${url}" title="${opts.title}" target="_blank">${text}</a>`;
return `<a href="${url instanceof URL ? url.href : url}" title="${opts?.title}" target="_blank">${text}</a>`;
}

return `[${text || url}](${url || "#"}${opts?.title ? ` "${opts.title}"` : ""})`;
}

Expand Down
8 changes: 8 additions & 0 deletions test/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ const renderTests = {
// No link
["", "Title", "[Title](#)"],
["", "", "[](#)"],
// Relative path
["./src/markdown.md", "Markdown", "[Markdown](./src/markdown.md)"],
// URL
[
new URL("https://www.example.com/"),
"Example",
"[Example](https://www.example.com/)",
],
],
list: [
[["Item 1", "Item 2"], "- Item 1\n- Item 2"],
Expand Down

0 comments on commit 9a3d151

Please sign in to comment.