Skip to content

Commit

Permalink
Merge branch 'master' into perf/binary-relation-index
Browse files Browse the repository at this point in the history
  • Loading branch information
uiolee authored Jan 10, 2025
2 parents cddd33a + bcfb030 commit 3825ebe
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/hexo/validate_config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'assert';
import type Hexo from './index';

export = (ctx: Hexo): void => {
Expand All @@ -12,6 +13,8 @@ export = (ctx: Hexo): void => {
try {
// eslint-disable-next-line no-new
new URL(config.url);
// eslint-disable-next-line no-new
assert(new URL(config.url).protocol.startsWith('http'));
} catch {
throw new TypeError('Invalid config detected: "url" should be a valid URL!');
}
Expand Down
36 changes: 26 additions & 10 deletions lib/plugins/filter/after_render/external_link.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { isExternalLink } from 'hexo-util';
import Hexo from '../../../hexo';
import type Hexo from '../../../hexo';

let EXTERNAL_LINK_SITE_ENABLED = true;
const rATag = /<a(?:\s+?|\s+?[^<>]+?\s+?)href=["']((?:https?:|\/\/)[^<>"']+)["'][^<>]*>/gi;
const rTargetAttr = /target=/i;
const rRelAttr = /rel=/i;
const rRelStrAttr = /rel=["']([^<>"']*)["']/i;

const addNoopener = (relStr: string, rel: string) => {
return rel.includes('noopenner') ? relStr : `rel="${rel} noopener"`;
};

function externalLinkFilter(this: Hexo, data: string): string {
if (!EXTERNAL_LINK_SITE_ENABLED) return;

Expand All @@ -17,18 +21,30 @@ function externalLinkFilter(this: Hexo, data: string): string {
return;
}

return data.replace(rATag, (str, href) => {
if (!isExternalLink(href, url, external_link.exclude as any) || rTargetAttr.test(str)) return str;
let result = '';
let lastIndex = 0;
let match;

while ((match = rATag.exec(data)) !== null) {
result += data.slice(lastIndex, match.index);

if (rRelAttr.test(str)) {
str = str.replace(rRelStrAttr, (relStr, rel) => {
return rel.includes('noopenner') ? relStr : `rel="${rel} noopener"`;
});
return str.replace('href=', 'target="_blank" href=');
const str = match[0];
const href = match[1];

if (!isExternalLink(href, url, external_link.exclude as any) || rTargetAttr.test(str)) {
result += str;
} else {
if (rRelAttr.test(str)) {
result += str.replace(rRelStrAttr, addNoopener).replace('href=', 'target="_blank" href=');
} else {
result += str.replace('href=', 'target="_blank" rel="noopener" href=');
}
}
lastIndex = rATag.lastIndex;
}
result += data.slice(lastIndex);

return str.replace('href=', 'target="_blank" rel="noopener" href=');
});
return result;
}

export = externalLinkFilter;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"build": "tsc -b",
"clean": "tsc -b --clean",
"eslint": "eslint lib test",
"pretest": "npm run clean && npm run build",
"test": "mocha test/scripts/**/*.ts --require ts-node/register",
"test-cov": "c8 --reporter=lcovonly npm test -- --no-parallel",
"prepare": "husky"
Expand Down
5 changes: 4 additions & 1 deletion test/scripts/box/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ describe('File', () => {
params: {foo: 'bar'}
});

before(async () => {
// NOTE: Do not use `arrow function` here.
// See https://mochajs.org/#arrow-functions
before(async function() {
this.timeout(20000);
await Promise.all([
writeFile(file.source, body),
hexo.init()
Expand Down
14 changes: 14 additions & 0 deletions test/scripts/hexo/validate_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ describe('Validate config', () => {
}
});


it('config.url - not start with xx://', () => {
// @ts-ignore
hexo.config.url = 'localhost:4000';

try {
validateConfig(hexo);
should.fail();
} catch (e) {
e.name.should.eql('TypeError');
e.message.should.eql('Invalid config detected: "url" should be a valid URL!');
}
});

// #4510
it('config.url - slash', () => {
hexo.config.url = '/';
Expand Down

0 comments on commit 3825ebe

Please sign in to comment.