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

second hyperlink in header line is errantly recreated in Table of Contents #908

Closed
jtmoon79 opened this issue Mar 5, 2021 · 2 comments · Fixed by #890
Closed

second hyperlink in header line is errantly recreated in Table of Contents #908

jtmoon79 opened this issue Mar 5, 2021 · 2 comments · Fixed by #890
Labels
Res: Duplicate This issue or pull request already exists.

Comments

@jtmoon79
Copy link

jtmoon79 commented Mar 5, 2021

Using Markdown All in One version v3.4.0.

Problem

Adding an additional hyperlink in a header line causes the printed to Table Of Contents entry with a raw markdown hyperlink.

How to reproduce

Given the following markdown with two markdown hyperlinks within the header

# my header1 [link1](http://www.link1.com) [link2](http://www.link2.com)

hello

Save this in a .md file.
From the Command Pallette, run Markdown All in One: Create Table of Contents. The link2 is a raw markdown hyperlink

# my header1 [link1](http://www.link1.com) [link2](http://www.link2.com)

- [# my header1 link1 [link2](http://www.link2.com)](#-my-header1-link1-link2)

hello
@yzhang-gh
Copy link
Owner

Thanks for the feedback. Can you please try out the dev build? I cannot remember when but it should already be fixed 😅.

@yzhang-gh yzhang-gh added the Res: Duplicate This issue or pull request already exists. label Mar 5, 2021
@Lemmingh
Copy link
Collaborator

Lemmingh commented Mar 5, 2021

First, #877

.replace(/\[([^\]]*?)\]\([^\)]*?\)/g, '$1') // Extract the link text from inline link. `[text](link)` → `text`

Finally, #890

vscode-markdown/src/toc.ts

Lines 452 to 478 in 51ed4fb

function createLinkText(raw: string, env: object): string {
const inlineTokens: Token[] = commonMarkEngine.engine.parseInline(raw, env)[0].children!;
return inlineTokens.reduce<string>((result, token) => {
switch (token.type) {
case "text":
return result + token.content.replace(/[&*<>\[\\\]_`]/g, "\\$&"); // Escape.
case "code_inline":
return result + token.markup + token.content + token.markup; // Emit as is.
case "strong_open":
case "strong_close":
case "em_open":
case "em_close":
return result + token.markup; // Preserve emphasis indicators.
case "link_open":
case "link_close":
case "image":
case "html_inline":
return result; // Discard them.
case "softbreak":
case "hardbreak":
return result + " "; // Replace line breaks with spaces.
default:
return result + token.content;
}
}, "");
}

@Lemmingh Lemmingh closed this as completed Mar 5, 2021
@Lemmingh Lemmingh linked a pull request Mar 5, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Res: Duplicate This issue or pull request already exists.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants