Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for title on links #33

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading