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

fix(anchor): ignore anchor hash when parsing internal link syntax #576

Merged
merged 1 commit into from
Sep 7, 2022
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
2 changes: 2 additions & 0 deletions __tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ exports[`anchors with baseUrl 1`] = `
<a href=\\"page:slug\\">page</a></p>"
`;

exports[`anchors with baseUrl and special characters in url hash 1`] = `"<p><a href=\\"/reference-link/slug#%E6%95%B4\\" target=\\"\\" title=\\"\\">ref</a></p>"`;

exports[`check list items 1`] = `
"<ul class=\\"contains-task-list\\">
<li class=\\"task-list-item\\"><input type=\\"checkbox\\" disabled=\\"\\"> checklistitem1</li>
Expand Down
5 changes: 5 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ test('anchors with baseUrl', () => {
expect(container.innerHTML).toMatchSnapshot();
});

test('anchors with baseUrl and special characters in url hash', () => {
const { container } = render(markdown.default('[ref](ref:slug#整)'));
expect(container.innerHTML).toMatchSnapshot();
});

test('emojis', () => {
const { container } = render(
markdown.default(`
Expand Down
22 changes: 13 additions & 9 deletions components/Anchor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@ const BaseUrlContext = require('../contexts/BaseUrl');
// Nabbed from here:
// https://github.com/readmeio/api-explorer/blob/0dedafcf71102feedaa4145040d3f57d79d95752/packages/api-explorer/src/lib/markdown/renderer.js#L52
function getHref(href, baseUrl) {
const [path, hash] = href.split('#');
const hashStr = hash ? `#${hash}` : '';

const base = baseUrl === '/' ? '' : baseUrl;
const doc = href.match(/^doc:([-_a-zA-Z0-9#]*)$/);
const doc = path.match(/^doc:([-_a-zA-Z0-9#]*)$/);

if (doc) {
return `${base}/docs/${doc[1]}`;
return `${base}/docs/${doc[1]}${hashStr}`;
}

const ref = href.match(/^ref:([-_a-zA-Z0-9#]*)$/);
const ref = path.match(/^ref:([-_a-zA-Z0-9#]*)$/);
if (ref) {
return `${base}/reference-link/${ref[1]}`;
return `${base}/reference-link/${ref[1]}${hashStr}`;
}

// we need to perform two matches for changelogs in case
// of legacy links that use 'blog' instead of 'changelog'
const blog = href.match(/^blog:([-_a-zA-Z0-9#]*)$/);
const changelog = href.match(/^changelog:([-_a-zA-Z0-9#]*)$/);
const blog = path.match(/^blog:([-_a-zA-Z0-9#]*)$/);
const changelog = path.match(/^changelog:([-_a-zA-Z0-9#]*)$/);
const changelogMatch = blog || changelog;
if (changelogMatch) {
return `${base}/changelog/${changelogMatch[1]}`;
return `${base}/changelog/${changelogMatch[1]}${hashStr}`;
}

const custompage = href.match(/^page:([-_a-zA-Z0-9#]*)$/);
const custompage = path.match(/^page:([-_a-zA-Z0-9#]*)$/);
if (custompage) {
return `${base}/page/${custompage[1]}`;
return `${base}/page/${custompage[1]}${hashStr}`;
}

return href;
Expand Down