Skip to content

Commit b817e91

Browse files
authored
Merge pull request #78 from posthtml/milestone-0.10.0
Milestone 0.10.0
2 parents 61be6d5 + 53bee3d commit b817e91

File tree

8 files changed

+66
-60
lines changed

8 files changed

+66
-60
lines changed

changelog.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
## 0.10.0 (2021-07-28)
2+
3+
* refactor: parse now export not default ([f8ae42d](https://github.com/posthtml/posthtml-parser/commit/f8ae42d))
4+
* perf: resolve types ([9ed9bd0](https://github.com/posthtml/posthtml-parser/commit/9ed9bd0))
5+
6+
7+
18
## <small>0.9.1 (2021-07-26)</small>
29

10+
* 0.9.1 ([a03017e](https://github.com/posthtml/posthtml-parser/commit/a03017e))
11+
* Update readme.md ([7081062](https://github.com/posthtml/posthtml-parser/commit/7081062))
312
* test: dom nest parsing issues #76 ([fdfe82e](https://github.com/posthtml/posthtml-parser/commit/fdfe82e)), closes [#76](https://github.com/posthtml/posthtml-parser/issues/76)
413
* test: remove only ([78b7fc7](https://github.com/posthtml/posthtml-parser/commit/78b7fc7))
514
* style: after prettier ([ccbfd69](https://github.com/posthtml/posthtml-parser/commit/ccbfd69))
@@ -10,7 +19,6 @@
1019
* build(deps): bump lodash from 4.17.20 to 4.17.21 ([e9b8d04](https://github.com/posthtml/posthtml-parser/commit/e9b8d04))
1120
* build(deps): bump normalize-url from 4.5.0 to 4.5.1 ([9e0d1f9](https://github.com/posthtml/posthtml-parser/commit/9e0d1f9))
1221
* perf: type for replace ([94ebb8d](https://github.com/posthtml/posthtml-parser/commit/94ebb8d))
13-
* Update readme.md ([7081062](https://github.com/posthtml/posthtml-parser/commit/7081062))
1422

1523

1624

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "posthtml-parser",
3-
"version": "0.9.1",
3+
"version": "0.10.0",
44
"description": "Parse HTML/XML to PostHTMLTree",
55
"license": "MIT",
66
"repository": "posthtml/posthtml-parser",
@@ -25,7 +25,7 @@
2525
},
2626
"scripts": {
2727
"version": "conventional-changelog -i changelog.md -s -r 0 && git add changelog.md",
28-
"build": "rm -rf dist && tsup src/*.ts --minify",
28+
"build": "rm -rf dist && tsup src/*.ts --dts --minify",
2929
"dev": "npm run build -- --watch",
3030
"test": "xo && c8 ava",
3131
"pretest": "clinton",
@@ -62,5 +62,5 @@
6262
"typescript": "^4.0.5",
6363
"xo": "^0.37.1"
6464
},
65-
"types": "types/index.d.ts"
65+
"types": "dist/index.d.ts"
6666
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $ npm install posthtml-parser
2222
</a>
2323
```
2424
```js
25-
import parser from 'posthtml-parser'
25+
import { parser } from 'posthtml-parser'
2626
import fs from 'fs'
2727

2828
const html = fs.readFileSync('path/to/input.html', 'utf-8')

src/index.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
import { Parser, ParserOptions } from 'htmlparser2';
2-
import { Directive, Node, NodeTag, Options, Attributes } from '../types/index.d';
3-
import { LocationTracker } from './location-tracker';
2+
import { LocationTracker, SourceLocation } from './location-tracker';
3+
4+
export type Directive = {
5+
name: string | RegExp;
6+
start: string;
7+
end: string;
8+
};
9+
10+
export type Options = {
11+
directives?: Directive[];
12+
sourceLocations?: boolean;
13+
} & ParserOptions;
14+
15+
export type Tag = string | boolean;
16+
export type Attributes = Record<string, string | number | boolean>;
17+
export type Content = NodeText | Array<Node | Node[]>;
18+
19+
export type NodeText = string | number;
20+
export type NodeTag = {
21+
tag?: Tag;
22+
attrs?: Attributes;
23+
content?: Content;
24+
location?: SourceLocation;
25+
};
26+
27+
export type Node = NodeText | NodeTag;
428

529
const defaultOptions: ParserOptions = {
630
lowerCaseTags: false,
@@ -16,7 +40,7 @@ const defaultDirectives: Directive[] = [
1640
}
1741
];
1842

19-
const parser = (html: string, options: Options = {}): Node[] => {
43+
export const parser = (html: string, options: Options = {}): Node[] => {
2044
const locationTracker = new LocationTracker(html);
2145
const bufArray: Node[] = [];
2246
const results: Node[] = [];
@@ -53,7 +77,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
5377

5478
function onprocessinginstruction(name: string, data: string) {
5579
const directives = defaultDirectives.concat(options.directives ?? []);
56-
const last: Node = bufferArrayLast();
80+
const last = bufferArrayLast();
5781

5882
for (const directive of directives) {
5983
const directiveText = directive.start + data + directive.end;
@@ -69,15 +93,17 @@ const parser = (html: string, options: Options = {}): Node[] => {
6993
last.content = [];
7094
}
7195

72-
last.content.push(directiveText);
96+
if (Array.isArray(last.content)) {
97+
last.content.push(directiveText);
98+
}
7399
}
74100
}
75101
}
76102
}
77103

78104
function oncomment(data: string) {
79-
const comment = `<!--${data}-->`;
80105
const last = bufferArrayLast();
106+
const comment = `<!--${data}-->`;
81107

82108
if (last === undefined) {
83109
results.push(comment);
@@ -89,7 +115,9 @@ const parser = (html: string, options: Options = {}): Node[] => {
89115
last.content = [];
90116
}
91117

92-
last.content.push(comment);
118+
if (Array.isArray(last.content)) {
119+
last.content.push(comment);
120+
}
93121
}
94122
}
95123

@@ -131,7 +159,9 @@ const parser = (html: string, options: Options = {}): Node[] => {
131159
last.content = [];
132160
}
133161

134-
last.content.push(buf);
162+
if (Array.isArray(last.content)) {
163+
last.content.push(buf);
164+
}
135165
}
136166
}
137167
}
@@ -145,7 +175,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
145175
}
146176

147177
if (typeof last === 'object') {
148-
if (last.content && last.content.length > 0) {
178+
if (last.content && Array.isArray(last.content) && last.content.length > 0) {
149179
const lastContentNode = last.content[last.content.length - 1];
150180
if (typeof lastContentNode === 'string' && !lastContentNode.startsWith('<!--')) {
151181
last.content[last.content.length - 1] = `${lastContentNode}${text}`;
@@ -157,7 +187,9 @@ const parser = (html: string, options: Options = {}): Node[] => {
157187
last.content = [];
158188
}
159189

160-
last.content.push(text);
190+
if (Array.isArray(last.content)) {
191+
last.content.push(text);
192+
}
161193
}
162194
}
163195

@@ -174,5 +206,3 @@ const parser = (html: string, options: Options = {}): Node[] => {
174206

175207
return results;
176208
};
177-
178-
export default parser;

src/location-tracker.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Position } from '../types/index.d';
1+
export type SourceLocation = {
2+
start: Position;
3+
end: Position;
4+
};
5+
6+
export type Position = {
7+
line: number;
8+
column: number;
9+
};
210

311
export class LocationTracker {
412
private readonly source: string;

test/test-core.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import parser from '../src';
2+
import { parser } from '../src';
33

44
test('should be parse doctype in uppercase', t => {
55
const tree = parser('<!DOCTYPE html>');

types/index.d.ts

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

0 commit comments

Comments
 (0)