Skip to content

Commit fa35887

Browse files
Upgrade all code
1 parent 2709777 commit fa35887

32 files changed

+365
-305
lines changed

.eslintrc.cjs

+32-25
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
11
// @ts-nocheck
2-
const restrictedGlobals = require("confusing-browser-globals");
2+
const restrictedGlobals = require('confusing-browser-globals');
33

44
module.exports = {
55
root: true,
66
parserOptions: {
7-
ecmaVersion: "2020",
8-
sourceType: "module",
7+
ecmaVersion: '2020',
8+
sourceType: 'module',
99
},
1010
extends: [
1111
// Syntax and ~
12-
"eslint:recommended",
13-
"es/browser",
14-
"plugin:node/recommended",
12+
'eslint:recommended',
13+
'es/browser',
14+
'plugin:node/recommended',
1515

16-
"plugin:ava/recommended",
17-
"standard-jsdoc",
18-
"plugin:json/recommended",
16+
'plugin:ava/recommended',
17+
'standard-jsdoc',
18+
'plugin:json/recommended',
1919

2020
// Funny
21-
"plugin:you-dont-need-lodash-underscore/compatible",
22-
"plugin:unicorn/recommended",
21+
'plugin:you-dont-need-lodash-underscore/compatible',
22+
'plugin:unicorn/recommended',
2323

2424
// Global config
25-
"airbnb-base",
25+
'airbnb-base',
2626
],
2727
plugins: [
28-
"json",
29-
"no-loops",
30-
"unicorn",
31-
"async-await",
32-
"prefer-object-spread",
28+
'json',
29+
'no-loops',
30+
'unicorn',
31+
'async-await',
32+
'prefer-object-spread',
3333

34-
"simple-import-sort",
34+
'simple-import-sort',
3535
],
3636
env: {
3737
browser: true,
3838
es6: true,
3939
},
4040
globals: {
41-
Atomics: "readonly",
42-
SharedArrayBuffer: "readonly",
41+
Atomics: 'readonly',
42+
SharedArrayBuffer: 'readonly',
4343
},
4444
rules: {
45-
"prefer-object-spread/prefer-object-spread": 2,
46-
"no-restricted-globals": [2, ...restrictedGlobals],
45+
'prefer-object-spread/prefer-object-spread': 2,
46+
'no-restricted-globals': [2, ...restrictedGlobals],
4747

48-
"mocha/handle-done-callback": 0,
49-
"mocha/no-global-tests": 0,
50-
"mocha/valid-test-description": 0,
48+
'mocha/handle-done-callback': 0,
49+
'mocha/no-global-tests': 0,
50+
'mocha/valid-test-description': 0,
51+
'import/extensions': 0,
52+
'import/prefer-default-export': 0,
53+
'import/no-cycle': 0,
54+
'unicorn/no-null': 0,
55+
'no-underscore-dangle': 0,
56+
'unicorn/no-array-reduce': 0,
57+
'unicorn/no-array-callback-reference': 0,
5158
},
5259
};

.vscode/cli-html.code-workspace

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"folders": [
3+
{
4+
"path": ".."
5+
}
6+
]
7+
}

.vscode/launch.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "pwa-node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"skipFiles": [
12+
"<node_internals>/**"
13+
],
14+
"program": "${workspaceFolder}/bin/html.js",
15+
"args": ["1.html"]
16+
}
17+
]
18+
}

bin/html.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
import { createReadStream } from 'fs';
3+
import { createReadStream } from 'node:fs';
44
import concat from 'concat-stream';
55

66
import cliHtml from '../index.js';

examples/pre.html

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
<p><p><p><p><p>qwe</p></p></p></p></p>
2-
<hr>
3-
<pre><p><p><p><p><p>qwe</p></p></p></p></p></pre>
4-
<hr>
1+
<pre><p>qwe</p></pre>

index.js

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
import parse5 from 'parse5';
22

3-
import { filterAst } from './lib/utils.js';
3+
import { parse } from 'node-html-parser';
4+
import { filterAst, filterAst2 } from './lib/utils.js';
45
import { html } from './lib/tags/document.js';
56

67
const htmlToCli = (rawHTML) => {
78
// @type Object
89

9-
const document = parse5.parse(rawHTML);
10+
// const document = parse5.parse(rawHTML);
11+
const document2 = parse(rawHTML, {
12+
blockTextElements: {
13+
script: false,
14+
noscript: false,
15+
style: false,
16+
// pre: true,
17+
code: true,
18+
},
19+
});
1020

11-
// console.dir(
12-
// filterAst(document.childNodes[0].childNodes[1]),
13-
// { depth: null },
14-
// );
21+
// console.dir(filterAst(document.childNodes[0].childNodes[1]), { depth: null });
1522

16-
return `${(html(document, { pre: false, lineWidth: +(process.env.CLI_HTML_LINE_WIDTH || '120') }) || { value: '' }).value}\n`;
23+
// console.log("----");
24+
25+
// console.dir(filterAst2(document2), {
26+
// depth: null,
27+
// });
28+
29+
return `${
30+
(
31+
html(document2, {
32+
pre: false,
33+
lineWidth: +(process.env.CLI_HTML_LINE_WIDTH || '120'),
34+
}) || { value: '' }
35+
).value
36+
}\n`;
1737
};
1838

1939
export default htmlToCli;

lib/tag-helpers/blockTag.js

+29-32
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,56 @@
1-
/* eslint-disable unicorn/consistent-function-scoping */
2-
import tags from "../tags.js";
3-
import { getAttribute } from "../utils.js";
1+
import tags from '../tags.js';
2+
import { getAttribute } from '../utils.js';
43

5-
import { concatTwoBlockTags } from "../utils/concatBlockTags.js";
6-
import { concatTwoInlineTags } from "../utils/concatInlineTags.js";
7-
import { inlineToBlockTag } from "../utils/inlineToBlockTag.js";
4+
import { concatTwoBlockTags } from '../utils/concatBlockTags.js';
5+
import { concatTwoInlineTags } from '../utils/concatInlineTags.js';
6+
import { inlineToBlockTag } from '../utils/inlineToBlockTag.js';
87

9-
import wrapLineWidth from "../wrapLineWidth.js";
8+
import wrapLineWidth from '../wrapLineWidth.js';
109

10+
/**
11+
* @param wrapper
12+
* @param localContext
13+
*/
1114
export function blockTag(wrapper, localContext) {
1215
return (tag, context) => {
13-
const wrapFn = wrapper || ((argument) => argument);
16+
const wrapFunction = wrapper || ((argument) => argument);
1417

1518
if (!tag || !tag.childNodes) {
1619
return null;
1720
}
1821

19-
let liItemNumber = parseInt(getAttribute(tag, "start", "1"), 10);
22+
let liItemNumber = Number.parseInt(getAttribute(tag, 'start', '1'), 10);
2023
const value = tag.childNodes.reduce(
2124
(accumulator, node) => {
22-
if (!tags[node.nodeName]) {
23-
return accumulator;
24-
}
25+
const tagFunction = tags[node.rawTagName || '#text'] || tags.span;
2526

26-
const nodeTag = tags[node.nodeName](node, { ...context, liItemNumber });
27+
const nodeTag = tagFunction(node, { ...context, liItemNumber });
2728

28-
if (nodeTag == null) {
29+
if (nodeTag == undefined) {
2930
return accumulator;
3031
}
3132

32-
if (nodeTag.nodeName === "li") {
33+
if (nodeTag.rawTagName === 'li') {
3334
liItemNumber += 1;
3435
}
3536

36-
if (nodeTag.type === "inline") {
37+
if (nodeTag.type === 'inline') {
3738
return {
3839
block: accumulator.block,
3940
inline: concatTwoInlineTags(accumulator.inline, nodeTag),
4041
};
4142
}
4243

43-
if (accumulator.inline && accumulator.inline.value != null) {
44-
if (!context.pre) {
45-
accumulator.inline.value = wrapLineWidth(
46-
accumulator.inline.value,
47-
context
48-
);
49-
}
44+
if (accumulator.inline && accumulator.inline.value != undefined && !context.pre) {
45+
accumulator.inline.value = wrapLineWidth(
46+
accumulator.inline.value,
47+
context,
48+
);
5049
}
5150

5251
accumulator.block = concatTwoBlockTags(
5352
accumulator.block,
54-
inlineToBlockTag(accumulator.inline)
53+
inlineToBlockTag(accumulator.inline),
5554
);
5655

5756
accumulator.block = concatTwoBlockTags(accumulator.block, nodeTag);
@@ -64,18 +63,16 @@ export function blockTag(wrapper, localContext) {
6463
{
6564
block: null,
6665
inline: null,
67-
}
66+
},
6867
);
6968

70-
if (value.inline != null && value.inline.value != null) {
71-
if (!context.pre) {
72-
value.inline.value = wrapLineWidth(value.inline.value, context);
73-
}
69+
if (value.inline != undefined && value.inline.value != undefined && !context.pre) {
70+
value.inline.value = wrapLineWidth(value.inline.value, context);
7471
}
7572

7673
value.block = concatTwoBlockTags(
7774
value.block,
78-
inlineToBlockTag(value.inline)
75+
inlineToBlockTag(value.inline),
7976
);
8077

8178
if (!value.block || !value.block.value) {
@@ -95,12 +92,12 @@ export function blockTag(wrapper, localContext) {
9592
value.block.marginTop && value.block.marginTop > topBlock
9693
? value.block.marginTop
9794
: topBlock,
98-
value: wrapFn(value.block.value, tag),
95+
value: wrapFunction(value.block.value, tag),
9996
marginBottom:
10097
value.block.marginBottom && value.block.marginBottom > bottomBlock
10198
? value.block.marginBottom
10299
: bottomBlock,
103-
type: "block",
100+
type: 'block',
104101
nodeName: tag.nodeName,
105102
};
106103
};

lib/tag-helpers/inlineTag.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
/* eslint-disable unicorn/consistent-function-scoping */
21
import tags from '../tags.js';
32

43
import { concatTwoInlineTags } from '../utils/concatInlineTags.js';
54

65
const inlineTag = (wrapper) => (tag, context) => {
7-
const wrapFn = wrapper || ((argument) => argument);
6+
const wrapFunction = wrapper || ((argument) => argument);
87

98
if (!tag || !tag.childNodes) {
109
return null;
1110
}
1211

1312
const value = tag.childNodes.reduce((accumulator, node) => {
14-
if (!tags[node.nodeName]) {
15-
return accumulator;
16-
}
13+
const tagFunction = tags[node.rawTagName || '#text'] || tags.span;
1714

18-
const nodeTag = tags[node.nodeName](node, context);
15+
const nodeTag = tagFunction(node, context);
1916

2017
if (nodeTag === null) {
2118
return accumulator;
@@ -24,7 +21,7 @@ const inlineTag = (wrapper) => (tag, context) => {
2421
return {
2522
...concatTwoInlineTags(accumulator, nodeTag),
2623
type: 'inline',
27-
nodeName: tag.nodeName,
24+
nodeName: tag.rawTagName,
2825
};
2926
}, null);
3027

@@ -34,9 +31,9 @@ const inlineTag = (wrapper) => (tag, context) => {
3431

3532
if (value.value) {
3633
return {
37-
pre: value.pre ? wrapFn(value.pre, tag) : null,
38-
value: value.value ? wrapFn(value.value, tag) : null,
39-
post: value.post ? wrapFn(value.post, tag) : null,
34+
pre: value.pre ? wrapFunction(value.pre, tag) : null,
35+
value: value.value ? wrapFunction(value.value, tag) : null,
36+
post: value.post ? wrapFunction(value.post, tag) : null,
4037
type: 'inline',
4138
nodeName: tag.nodeName,
4239
};

lib/tag-helpers/textNode.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,24 @@ export const textNode = (tag, context) => {
55
if (context.pre) {
66
return {
77
pre: null,
8-
value: tag.value,
8+
value: tag._rawText,
99
post: null,
10-
type: "inline",
11-
nodeName: "#text",
10+
type: 'inline',
11+
nodeName: '#text',
1212
};
1313
}
1414
const normalized = context.pre
15-
? tag.value.split('')
16-
: normalizeWhitespace(tag.value)
17-
.replace(/\n/g, ' ')
18-
.split('');
15+
? [...tag._rawText]
16+
: [...normalizeWhitespace(tag._rawText).replace(/\n/g, ' ')];
1917

2018
const pre = [' ', '\n'].includes(normalized[0]) ? normalized.shift() : null;
2119
const post = [' ', '\n'].includes(normalized[normalized.length - 1]) ? normalized.pop() : null;
2220

2321
return {
2422
pre,
25-
value: normalized.length > 0 ? `${he.decode(normalized.join(""))}` : null,
23+
value: normalized.length > 0 ? `${he.decode(normalized.join(''))}` : null,
2624
post,
27-
type: "inline",
28-
nodeName: "#text",
25+
type: 'inline',
26+
nodeName: '#text',
2927
};
3028
};

0 commit comments

Comments
 (0)