Skip to content

Commit

Permalink
Add support for title on links (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashm authored Sep 25, 2024
1 parent 2e7dab2 commit e179ed1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type MarkdownNodeMap = {
},
link: {
href: string,
title?: string,
children: MarkdownNode,
},
image: {
Expand Down
3 changes: 2 additions & 1 deletion src/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,14 @@ class MarkdownParser {

this.match('](');

const href = this.parseText(')');
const [href, titleWithEndQuote] = this.parseText(')').split(/\s+"/);

this.match(')');

return {
type: 'link',
href: href,
title: titleWithEndQuote?.slice(0, -1) /* remove quote character at the end */,
children: label,
source: this.getSlice(startIndex, this.index),
};
Expand Down
2 changes: 2 additions & 0 deletions src/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type VisitedMarkdownNodeMap<C> = {
},
link: {
href: string,
title?: string,
children: C,
},
image: {
Expand Down Expand Up @@ -92,6 +93,7 @@ function visit<T>(node: MarkdownNode, visitor: MarkdownRenderer<T>): T {
return visitor.link({
type: node.type,
href: node.href,
title: node.title,
children: visit(node.children, visitor),
source: node.source,
});
Expand Down
30 changes: 30 additions & 0 deletions test/parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,36 @@ describe('A Markdown parser function', () => {
],
},
},
'link with title': {
input: 'Hello, [world](image.png "The world")!',
output: {
type: 'fragment',
source: 'Hello, [world](image.png "The world")!',
children: [
{
type: 'text',
source: 'Hello, ',
content: 'Hello, ',
},
{
type: 'link',
source: '[world](image.png "The world")',
href: 'image.png',
title: 'The world',
children: {
type: 'text',
source: 'world',
content: 'world',
},
},
{
type: 'text',
source: '!',
content: '!',
},
],
},
},
image: {
input: 'Hello, ![world](image.png)!',
output: {
Expand Down
19 changes: 19 additions & 0 deletions test/rendering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('A Markdown render function', () => {
type: 'link',
source: node.source,
href: node.href,
title: node.title,
children: node.children,
};
}
Expand All @@ -86,6 +87,7 @@ describe('A Markdown render function', () => {
'`Code`',
'![Image](https://example.com/image.png)',
'[Link](https://example.com)',
'[Link with title](https://example.com "Link title")',
].join('\n\n');

const tree: MarkdownNode = {
Expand Down Expand Up @@ -195,6 +197,23 @@ describe('A Markdown render function', () => {
},
],
},
{
type: 'paragraph',
source: '[Link with title](https://example.com "Link title")',
children: [
{
type: 'link',
source: '[Link with title](https://example.com "Link title")',
href: 'https://example.com',
title: 'Link title',
children: {
type: 'text',
source: 'Link with title',
content: 'Link with title',
},
},
],
},
],
};

Expand Down

0 comments on commit e179ed1

Please sign in to comment.