Skip to content

Commit ca1147d

Browse files
authored
Merge branch 'master' into dependabot/npm_and_yarn/chai-6.2.0
2 parents a99e56e + ba23651 commit ca1147d

23 files changed

+140
-149
lines changed

.eslintrc.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# hexo-util
22

3-
[![Build Status](https://github.com/hexojs/hexo-util/workflows/Tester/badge.svg?branch=master)](https://github.com/hexojs/hexo-util/actions?query=workflow%3ATester)
3+
[![Build Status](https://github.com/hexojs/hexo-util/workflows/Tester/badge.svg)](https://github.com/hexojs/hexo-util/actions?query=workflow%3ATester)
44
[![NPM version](https://badge.fury.io/js/hexo-util.svg)](https://www.npmjs.com/package/hexo-util)
55
[![Coverage Status](https://coveralls.io/repos/hexojs/hexo-util/badge.svg?branch=master&service=github)](https://coveralls.io/github/hexojs/hexo-util?branch=master)
66

eslint.config.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const config = require('eslint-config-hexo/ts');
2+
const testConfig = require('eslint-config-hexo/test');
3+
4+
module.exports = [
5+
// Configurations applied globally
6+
...config,
7+
{
8+
rules: {
9+
'@typescript-eslint/no-explicit-any': 0,
10+
'@typescript-eslint/no-var-requires': 0,
11+
'@typescript-eslint/no-require-imports': 0,
12+
'n/no-missing-import': 0,
13+
'n/no-missing-require': 0
14+
}
15+
},
16+
// Configurations applied only to test files
17+
{
18+
files: [
19+
'test/**/*.ts'
20+
],
21+
languageOptions: {
22+
...testConfig.languageOptions
23+
},
24+
rules: {
25+
...testConfig.rules,
26+
'@typescript-eslint/no-explicit-any': 0,
27+
'@typescript-eslint/no-unused-expressions': 0
28+
}
29+
}
30+
];

lib/decode_url.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// eslint-disable-next-line n/no-deprecated-api
12
import { parse, format } from 'url';
23
import { unescape } from 'querystring';
34

lib/encode_url.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// eslint-disable-next-line n/no-deprecated-api
12
import { parse, format } from 'url';
23
import { unescape } from 'querystring';
34

lib/full_url_for.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// eslint-disable-next-line n/no-deprecated-api
12
import { parse } from 'url';
23
import encodeURL from './encode_url';
34
import prettyUrls from './pretty_urls';

lib/highlight.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { HLJSApi, HighlightResult } from 'highlight.js';
22
import stripIndent from 'strip-indent';
3-
// eslint-disable-next-line @typescript-eslint/no-var-requires
3+
44
const alias = require('../highlight_alias.json');
55

66
let hljs: HLJSApi | undefined;

lib/is_external_link.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// eslint-disable-next-line n/no-deprecated-api
12
import { parse } from 'url';
23
import Cache from './cache';
34
const cache = new Cache<boolean>();
@@ -23,7 +24,7 @@ function isExternalLink(input: string, sitehost: string, exclude?: string | stri
2324
let data;
2425
try {
2526
data = new URL(input, `http://${sitehost}`);
26-
} catch (e) { }
27+
} catch { }
2728

2829
// if input is invalid url, data should be undefined
2930
if (typeof data !== 'object') return false;

lib/strip_html.ts

Lines changed: 71 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,102 @@
1-
const STATE_PLAINTEXT = Symbol('plaintext');
2-
const STATE_HTML = Symbol('html');
3-
const STATE_COMMENT = Symbol('comment');
4-
5-
// eslint-disable-next-line @typescript-eslint/ban-types
1+
const STATE_PLAINTEXT = 0;
2+
const STATE_HTML = 1;
3+
const STATE_COMMENT = 2;
4+
5+
const CHAR_LT = 60; // '<'
6+
const CHAR_GT = 62; // '>'
7+
const CHAR_QUOTE = 34; // '"'
8+
const CHAR_APOS = 39; // "'"
9+
const CHAR_DASH = 45; // '-'
10+
const CHAR_SPACE = 32; // ' '
11+
const CHAR_NEWLINE = 10; // '\n'
12+
const CHAR_EXCLAIM = 33; // '!'
13+
14+
// eslint-disable-next-line @typescript-eslint/no-wrapper-object-types
615
function striptags(html: string | String = '') {
716
// if not string, then safely return an empty string
817
if (typeof html !== 'string' && !(html instanceof String)) {
918
return '';
1019
}
1120

1221
let state = STATE_PLAINTEXT;
13-
let tag_buffer = '';
1422
let depth = 0;
15-
let in_quote_char = '';
23+
let in_quote_char = 0;
1624
let output = '';
25+
let tag_start = -1;
26+
let plain_text_start = 0;
1727

1828
const { length } = html;
1929

2030
for (let idx = 0; idx < length; idx++) {
21-
const char = html[idx];
31+
const charCode = html.charCodeAt(idx);
2232

2333
if (state === STATE_PLAINTEXT) {
24-
switch (char) {
25-
case '<':
26-
state = STATE_HTML;
27-
tag_buffer = tag_buffer + char;
28-
break;
29-
30-
default:
31-
output += char;
32-
break;
34+
if (charCode === CHAR_LT) {
35+
output += html.slice(plain_text_start, idx);
36+
state = STATE_HTML;
37+
tag_start = idx;
3338
}
3439
} else if (state === STATE_HTML) {
35-
switch (char) {
36-
case '<':
37-
// ignore '<' if inside a quote
38-
if (in_quote_char) break;
39-
40-
// we're seeing a nested '<'
41-
depth++;
42-
break;
43-
44-
case '>':
45-
// ignore '>' if inside a quote
46-
if (in_quote_char) {
47-
break;
48-
}
49-
50-
// something like this is happening: '<<>>'
40+
if (charCode === CHAR_LT) {
41+
// ignore '<' if inside a quote
42+
if (!in_quote_char) depth++;
43+
} else if (charCode === CHAR_GT) {
44+
// ignore '>' if inside a quote
45+
if (!in_quote_char) {
5146
if (depth) {
5247
depth--;
53-
54-
break;
55-
}
56-
57-
// this is closing the tag in tag_buffer
58-
in_quote_char = '';
59-
state = STATE_PLAINTEXT;
60-
// tag_buffer += '>';
61-
62-
tag_buffer = '';
63-
break;
64-
65-
case '"':
66-
case '\'':
67-
// catch both single and double quotes
68-
69-
if (char === in_quote_char) {
70-
in_quote_char = '';
7148
} else {
72-
in_quote_char = in_quote_char || char;
73-
}
74-
75-
tag_buffer = tag_buffer + char;
76-
break;
77-
78-
case '-':
79-
if (tag_buffer === '<!-') {
80-
state = STATE_COMMENT;
81-
}
82-
83-
tag_buffer = tag_buffer + char;
84-
break;
85-
86-
case ' ':
87-
case '\n':
88-
if (tag_buffer === '<') {
49+
// this is closing the tag in tag_buffer
50+
in_quote_char = 0;
8951
state = STATE_PLAINTEXT;
90-
output += '< ';
91-
tag_buffer = '';
92-
93-
break;
52+
tag_start = -1;
53+
plain_text_start = idx + 1;
9454
}
95-
96-
tag_buffer = tag_buffer + char;
97-
break;
98-
99-
default:
100-
tag_buffer = tag_buffer + char;
101-
break;
55+
}
56+
} else if (charCode === CHAR_QUOTE || charCode === CHAR_APOS) {
57+
// catch both single and double quotes
58+
59+
if (charCode === in_quote_char) {
60+
in_quote_char = 0;
61+
} else {
62+
in_quote_char = in_quote_char || charCode;
63+
}
64+
} else if (charCode === CHAR_DASH) {
65+
// same as if (html.slice(tag_start, idx) === '<!-') {
66+
if (idx - tag_start === 3
67+
&& html.charCodeAt(tag_start + 1) === CHAR_EXCLAIM
68+
&& html.charCodeAt(tag_start + 2) === CHAR_DASH
69+
) {
70+
state = STATE_COMMENT;
71+
}
72+
} else if (charCode === CHAR_SPACE || charCode === CHAR_NEWLINE) {
73+
// same as if (html.slice(tag_start, idx) === '<') {
74+
if (idx - tag_start === 1) {
75+
state = STATE_PLAINTEXT;
76+
output += '< ';
77+
tag_start = -1;
78+
plain_text_start = idx + 1;
79+
}
10280
}
10381
} else if (state === STATE_COMMENT) {
104-
switch (char) {
105-
case '>':
106-
if (tag_buffer.slice(-2) === '--') {
107-
// close the comment
108-
state = STATE_PLAINTEXT;
109-
}
110-
111-
tag_buffer = '';
112-
break;
113-
114-
default:
115-
tag_buffer = tag_buffer + char;
116-
break;
82+
if (charCode === CHAR_GT) {
83+
// same as if (html.slice(idx - 2, idx) === '--') {
84+
if (idx >= 2
85+
&& html.charCodeAt(idx - 1) === CHAR_DASH
86+
&& html.charCodeAt(idx - 2) === CHAR_DASH) {
87+
// close the comment
88+
state = STATE_PLAINTEXT;
89+
plain_text_start = idx + 1;
90+
}
91+
tag_start = -1;
11792
}
11893
}
11994
}
12095

96+
if (state === STATE_PLAINTEXT && plain_text_start < length) {
97+
output += html.slice(plain_text_start);
98+
}
99+
121100
return output;
122101
}
123102

lib/toc_obj.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { DomHandler, DomUtils, Parser } from 'htmlparser2';
2-
// eslint-disable-next-line node/no-extraneous-import
32
import type { Element } from 'domhandler';
43
import escapeHTML from './escape_html';
54
const nonWord = /^\s*[^a-zA-Z0-9]\s*$/;

0 commit comments

Comments
 (0)