Skip to content

Commit

Permalink
feat: replace relative URL with cloud.google.com (#72)
Browse files Browse the repository at this point in the history
Replace relative URL in description with cloud.google.com
  • Loading branch information
bcoe committed Jul 7, 2021
1 parent 8237055 commit 212febc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,23 @@ function parseSamples(content) {
}
}

const BASE_URL = 'https://cloud.google.com/';
function replaceRelativeLinks(description) {
return description.replace(/href="\//g, `href="${BASE_URL}`);
}

exports.handlers = {
newDoclet: e => {
if (sampleCache.size === 0) {
exports.loadSampleCache();
}

// Any relative links we observe, e.g., /iam, should be assumed to be
// relative to cloud.google.com:
if (e.doclet.description) {
e.doclet.description = replaceRelativeLinks(e.doclet.description);
}

const examples = e.doclet.examples;

if (!examples) {
Expand Down
42 changes: 41 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

process.env.SAMPLES_DIRECTORY = './test/fixtures';

const {loadSampleCache} = require('../src');
const {loadSampleCache, handlers} = require('../src');
const {describe, it} = require('mocha');
const assert = require('assert');

Expand All @@ -28,4 +28,44 @@ describe('jsdoc-region-tag', () => {
assert(sample.includes('async function createDataset () {'));
});
});
describe('handlers', () => {
it('does not replace absolute link', () => {
const doc = {
doclet: {
description:
'My description <a href="https://github.com/foo">foo link</a>',
},
};
handlers.newDoclet(doc);
assert.strictEqual(
doc.doclet.description,
'My description <a href="https://github.com/foo">foo link</a>'
);
});
it('replaces single link in description', () => {
const doc = {
doclet: {
description: 'My description <a href="/foo">foo link</a>',
},
};
handlers.newDoclet(doc);
assert.strictEqual(
doc.doclet.description,
'My description <a href="https://cloud.google.com/foo">foo link</a>'
);
});
it('replaces multiple links in description', () => {
const doc = {
doclet: {
description:
'My description <a href="/foo">foo link</a> hello <a href="/bar">bar link</a>',
},
};
handlers.newDoclet(doc);
assert.strictEqual(
doc.doclet.description,
'My description <a href="https://cloud.google.com/foo">foo link</a> hello <a href="https://cloud.google.com/bar">bar link</a>'
);
});
});
});

0 comments on commit 212febc

Please sign in to comment.