Skip to content

fix(site): Improve loaders parsing of external link in headers #2470

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

Closed
wants to merge 2 commits into from
Closed
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
33 changes: 26 additions & 7 deletions src/utilities/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ module.exports = function() {

// Patch IDs (this.options.headerPrefix can be undefined!)
renderer.heading = function(text, level, raw) {
var parsed = parseAnchor(raw);
var id = parsed.id;
let parsed = parseAnchor(raw);
let id = parsed.id;
let title = parsed.title;

let href = getHref(text);
let spanContent = href ? `<a href="${href}">${title}</a>` : title;

return (
`<h${level} class="header">
<a class="anchor" aria-hidden="true" href="#${id}" id="${id}"></a>
<span class="text">${text}</span>
<a aria-label="${text}" class="icon-link" href="#${id}"></a>
<span class="text">${spanContent}</span>
<a aria-label="${title}" class="icon-link" href="#${id}"></a>
</h${level}>\n`
);
};
Expand Down Expand Up @@ -143,13 +147,28 @@ function parseContent(data) {
return tokens;
}

/**
* Parses an anchor tag, and returns either hash link, or the external link
*
* This will parse Github links converted from markdown,
* so we can assume the output is quite consistent
*/
function getHref(anchor) {
let hrefRegex = new RegExp('href=(\'|")(.*)(\'|")');

if (hrefRegex.test(anchor)) {
return hrefRegex.exec(anchor)[2];
}
}

function parseAnchor(string) {
var stripped = string.replace(/\[(.+)\]\(.+\)/gi, '$1').replace(/(<([^>]+)>)/ig, '');
var clean = stripped.replace(/`/g, '');
let stripped = string.replace(/\[(.+)\]\(.+\)/gi, '$1').replace(/(<([^>]+)>)/ig, '');
let clean = stripped.replace(/`/g, '');
let id = clean.replace(/[^\w\u4e00-\u9fa5]+/g, '-').toLowerCase();

return {
title: clean,
id: clean.replace(/[^\w\u4e00-\u9fa5]+/g, '-').toLowerCase()
id: id
};
}

Expand Down