Skip to content
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