@peam-ai/parser 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +11 -0
- package/dist/index.d.mts +105 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +333 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +293 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import robotsParser from 'robots-parser';
|
|
2
|
+
|
|
3
|
+
interface StructuredPage {
|
|
4
|
+
title: string;
|
|
5
|
+
description: string;
|
|
6
|
+
content: string;
|
|
7
|
+
textContent: string;
|
|
8
|
+
contentLength?: number;
|
|
9
|
+
author?: string;
|
|
10
|
+
direction?: string;
|
|
11
|
+
language?: string;
|
|
12
|
+
siteName?: string;
|
|
13
|
+
publishedTime?: string;
|
|
14
|
+
ogTitle?: string;
|
|
15
|
+
ogDescription?: string;
|
|
16
|
+
ogImage?: string;
|
|
17
|
+
keywords?: string[];
|
|
18
|
+
headings?: string[];
|
|
19
|
+
internalLinks?: string[];
|
|
20
|
+
externalLinks?: string[];
|
|
21
|
+
images?: string[];
|
|
22
|
+
imageAlts?: string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface ParseOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Keep CSS classes in the parsed HTML content
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
keepClasses?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Disable JSON-LD extraction
|
|
33
|
+
* @default true
|
|
34
|
+
*/
|
|
35
|
+
disableJSONLD?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Extract internal links
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
extractLinks?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Extract images
|
|
43
|
+
* @default true
|
|
44
|
+
*/
|
|
45
|
+
extractImages?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Checks if a path matches any of the given wildcard patterns
|
|
50
|
+
* Uses the matcher library which supports:
|
|
51
|
+
* - * (matches any characters except /)
|
|
52
|
+
* - ** (matches any characters including /)
|
|
53
|
+
* - ? (matches single character)
|
|
54
|
+
* - ! (negation)
|
|
55
|
+
* - [] (character ranges)
|
|
56
|
+
*/
|
|
57
|
+
declare function matchesExcludePattern(path: string, patterns: string[]): boolean;
|
|
58
|
+
|
|
59
|
+
type RobotsParser = ReturnType<typeof robotsParser>;
|
|
60
|
+
interface RobotsTxtResult {
|
|
61
|
+
parser: RobotsParser;
|
|
62
|
+
path: string;
|
|
63
|
+
}
|
|
64
|
+
declare function createRobotsParser(content: string): RobotsParser;
|
|
65
|
+
/**
|
|
66
|
+
* Loads and parses robots.txt from custom path or standard locations
|
|
67
|
+
* Returns the parser and the path where robots.txt was found
|
|
68
|
+
*/
|
|
69
|
+
declare function loadRobotsTxt(projectDir: string, searchPaths: string[], robotsTxtPath?: string): RobotsTxtResult | null;
|
|
70
|
+
declare function isPathAllowedByRobots(path: string, robots: RobotsParser | null): boolean;
|
|
71
|
+
|
|
72
|
+
type PathFilterReason = 'included' | 'dynamic-route' | 'internal-route' | 'rsc-file' | 'segment-file' | 'static-asset' | 'robots-txt' | 'exclude-pattern';
|
|
73
|
+
interface PathFilterResult {
|
|
74
|
+
included: boolean;
|
|
75
|
+
reason: PathFilterReason;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Determines if a pathname should be included in the index
|
|
79
|
+
* Returns both the decision and the reason for exclusion
|
|
80
|
+
*/
|
|
81
|
+
declare function shouldIncludePath(pathname: string, robots: RobotsParser | null, excludePatterns: string[], respectRobotsTxt: boolean): PathFilterResult;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Convert file path to URL pathname
|
|
85
|
+
*
|
|
86
|
+
* Examples:
|
|
87
|
+
* index.html -> /
|
|
88
|
+
* about.html -> /about
|
|
89
|
+
* about/index.html -> /about/
|
|
90
|
+
* blog/post-1.html -> /blog/post-1
|
|
91
|
+
* blog/post-1/index.html -> /blog/post-1/
|
|
92
|
+
* server/pages/contact.html -> /contact
|
|
93
|
+
* server/app/about.html -> /about
|
|
94
|
+
*/
|
|
95
|
+
declare function filePathToPathname(filePath: string): string;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Parse HTML content and convert it to a StructuredPage
|
|
99
|
+
* @param html - HTML string to parse
|
|
100
|
+
* @param options - Parsing options
|
|
101
|
+
* @returns StructuredPage or undefined if parsing fails
|
|
102
|
+
*/
|
|
103
|
+
declare function parseHTML(html: string, options?: ParseOptions): StructuredPage | undefined;
|
|
104
|
+
|
|
105
|
+
export { type ParseOptions, type PathFilterReason, type PathFilterResult, type RobotsParser, type RobotsTxtResult, type StructuredPage, createRobotsParser, filePathToPathname, isPathAllowedByRobots, loadRobotsTxt, matchesExcludePattern, parseHTML, shouldIncludePath };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import robotsParser from 'robots-parser';
|
|
2
|
+
|
|
3
|
+
interface StructuredPage {
|
|
4
|
+
title: string;
|
|
5
|
+
description: string;
|
|
6
|
+
content: string;
|
|
7
|
+
textContent: string;
|
|
8
|
+
contentLength?: number;
|
|
9
|
+
author?: string;
|
|
10
|
+
direction?: string;
|
|
11
|
+
language?: string;
|
|
12
|
+
siteName?: string;
|
|
13
|
+
publishedTime?: string;
|
|
14
|
+
ogTitle?: string;
|
|
15
|
+
ogDescription?: string;
|
|
16
|
+
ogImage?: string;
|
|
17
|
+
keywords?: string[];
|
|
18
|
+
headings?: string[];
|
|
19
|
+
internalLinks?: string[];
|
|
20
|
+
externalLinks?: string[];
|
|
21
|
+
images?: string[];
|
|
22
|
+
imageAlts?: string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface ParseOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Keep CSS classes in the parsed HTML content
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
keepClasses?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Disable JSON-LD extraction
|
|
33
|
+
* @default true
|
|
34
|
+
*/
|
|
35
|
+
disableJSONLD?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Extract internal links
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
extractLinks?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Extract images
|
|
43
|
+
* @default true
|
|
44
|
+
*/
|
|
45
|
+
extractImages?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Checks if a path matches any of the given wildcard patterns
|
|
50
|
+
* Uses the matcher library which supports:
|
|
51
|
+
* - * (matches any characters except /)
|
|
52
|
+
* - ** (matches any characters including /)
|
|
53
|
+
* - ? (matches single character)
|
|
54
|
+
* - ! (negation)
|
|
55
|
+
* - [] (character ranges)
|
|
56
|
+
*/
|
|
57
|
+
declare function matchesExcludePattern(path: string, patterns: string[]): boolean;
|
|
58
|
+
|
|
59
|
+
type RobotsParser = ReturnType<typeof robotsParser>;
|
|
60
|
+
interface RobotsTxtResult {
|
|
61
|
+
parser: RobotsParser;
|
|
62
|
+
path: string;
|
|
63
|
+
}
|
|
64
|
+
declare function createRobotsParser(content: string): RobotsParser;
|
|
65
|
+
/**
|
|
66
|
+
* Loads and parses robots.txt from custom path or standard locations
|
|
67
|
+
* Returns the parser and the path where robots.txt was found
|
|
68
|
+
*/
|
|
69
|
+
declare function loadRobotsTxt(projectDir: string, searchPaths: string[], robotsTxtPath?: string): RobotsTxtResult | null;
|
|
70
|
+
declare function isPathAllowedByRobots(path: string, robots: RobotsParser | null): boolean;
|
|
71
|
+
|
|
72
|
+
type PathFilterReason = 'included' | 'dynamic-route' | 'internal-route' | 'rsc-file' | 'segment-file' | 'static-asset' | 'robots-txt' | 'exclude-pattern';
|
|
73
|
+
interface PathFilterResult {
|
|
74
|
+
included: boolean;
|
|
75
|
+
reason: PathFilterReason;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Determines if a pathname should be included in the index
|
|
79
|
+
* Returns both the decision and the reason for exclusion
|
|
80
|
+
*/
|
|
81
|
+
declare function shouldIncludePath(pathname: string, robots: RobotsParser | null, excludePatterns: string[], respectRobotsTxt: boolean): PathFilterResult;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Convert file path to URL pathname
|
|
85
|
+
*
|
|
86
|
+
* Examples:
|
|
87
|
+
* index.html -> /
|
|
88
|
+
* about.html -> /about
|
|
89
|
+
* about/index.html -> /about/
|
|
90
|
+
* blog/post-1.html -> /blog/post-1
|
|
91
|
+
* blog/post-1/index.html -> /blog/post-1/
|
|
92
|
+
* server/pages/contact.html -> /contact
|
|
93
|
+
* server/app/about.html -> /about
|
|
94
|
+
*/
|
|
95
|
+
declare function filePathToPathname(filePath: string): string;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Parse HTML content and convert it to a StructuredPage
|
|
99
|
+
* @param html - HTML string to parse
|
|
100
|
+
* @param options - Parsing options
|
|
101
|
+
* @returns StructuredPage or undefined if parsing fails
|
|
102
|
+
*/
|
|
103
|
+
declare function parseHTML(html: string, options?: ParseOptions): StructuredPage | undefined;
|
|
104
|
+
|
|
105
|
+
export { type ParseOptions, type PathFilterReason, type PathFilterResult, type RobotsParser, type RobotsTxtResult, type StructuredPage, createRobotsParser, filePathToPathname, isPathAllowedByRobots, loadRobotsTxt, matchesExcludePattern, parseHTML, shouldIncludePath };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __export = (target, all) => {
|
|
23
|
+
for (var name in all)
|
|
24
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
25
|
+
};
|
|
26
|
+
var __copyProps = (to, from, except, desc) => {
|
|
27
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
28
|
+
for (let key of __getOwnPropNames(from))
|
|
29
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
30
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
31
|
+
}
|
|
32
|
+
return to;
|
|
33
|
+
};
|
|
34
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
35
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
36
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
37
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
38
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
39
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
40
|
+
mod
|
|
41
|
+
));
|
|
42
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
43
|
+
|
|
44
|
+
// src/index.ts
|
|
45
|
+
var index_exports = {};
|
|
46
|
+
__export(index_exports, {
|
|
47
|
+
createRobotsParser: () => createRobotsParser,
|
|
48
|
+
filePathToPathname: () => filePathToPathname,
|
|
49
|
+
isPathAllowedByRobots: () => isPathAllowedByRobots,
|
|
50
|
+
loadRobotsTxt: () => loadRobotsTxt,
|
|
51
|
+
matchesExcludePattern: () => matchesExcludePattern,
|
|
52
|
+
parseHTML: () => parseHTML,
|
|
53
|
+
shouldIncludePath: () => shouldIncludePath
|
|
54
|
+
});
|
|
55
|
+
module.exports = __toCommonJS(index_exports);
|
|
56
|
+
|
|
57
|
+
// src/utils/excludePatterns.ts
|
|
58
|
+
var import_matcher = require("matcher");
|
|
59
|
+
function matchesExcludePattern(path, patterns) {
|
|
60
|
+
if (!patterns || patterns.length === 0) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
const normalize = (p) => p.startsWith("/") ? p : `/${p}`;
|
|
64
|
+
const normalizedPath = normalize(path);
|
|
65
|
+
const normalizedPatterns = patterns.map(normalize);
|
|
66
|
+
return (0, import_matcher.isMatch)(normalizedPath, normalizedPatterns);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/utils/robotsParser.ts
|
|
70
|
+
var import_fs = require("fs");
|
|
71
|
+
var import_path = require("path");
|
|
72
|
+
var import_robots_parser = __toESM(require("robots-parser"));
|
|
73
|
+
function createRobotsParser(content) {
|
|
74
|
+
return (0, import_robots_parser.default)("https://robots.invalid/robots.txt", content);
|
|
75
|
+
}
|
|
76
|
+
function loadRobotsTxt(projectDir, searchPaths, robotsTxtPath) {
|
|
77
|
+
let robotsContent = null;
|
|
78
|
+
let foundPath = null;
|
|
79
|
+
if (robotsTxtPath) {
|
|
80
|
+
const customPath = (0, import_path.join)(projectDir, robotsTxtPath);
|
|
81
|
+
if ((0, import_fs.existsSync)(customPath)) {
|
|
82
|
+
robotsContent = (0, import_fs.readFileSync)(customPath, "utf-8");
|
|
83
|
+
foundPath = customPath;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!robotsContent) {
|
|
87
|
+
for (const searchPath of searchPaths) {
|
|
88
|
+
const fullPath = (0, import_path.join)(projectDir, searchPath);
|
|
89
|
+
if ((0, import_fs.existsSync)(fullPath)) {
|
|
90
|
+
robotsContent = (0, import_fs.readFileSync)(fullPath, "utf-8");
|
|
91
|
+
foundPath = fullPath;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (!robotsContent) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
parser: createRobotsParser(robotsContent),
|
|
101
|
+
path: foundPath || ""
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function isPathAllowedByRobots(path, robots) {
|
|
105
|
+
if (!robots) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
109
|
+
const testUrl = `https://robots.invalid${normalizedPath}`;
|
|
110
|
+
const isAllowed = robots.isAllowed(testUrl, "*");
|
|
111
|
+
return isAllowed !== false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/utils/pathFilter.ts
|
|
115
|
+
function shouldIncludePath(pathname, robots, excludePatterns, respectRobotsTxt) {
|
|
116
|
+
if (pathname.includes("[") && pathname.includes("]")) {
|
|
117
|
+
return { included: false, reason: "dynamic-route" };
|
|
118
|
+
}
|
|
119
|
+
if (pathname.startsWith("/_not-found") || pathname.startsWith("/_global-error")) {
|
|
120
|
+
return { included: false, reason: "internal-route" };
|
|
121
|
+
}
|
|
122
|
+
if (pathname.endsWith(".rsc")) {
|
|
123
|
+
return { included: false, reason: "rsc-file" };
|
|
124
|
+
}
|
|
125
|
+
if (pathname.includes(".segments/")) {
|
|
126
|
+
return { included: false, reason: "segment-file" };
|
|
127
|
+
}
|
|
128
|
+
if (pathname.match(/\.(ico|png|jpg|jpeg|svg|gif|webp|txt|xml|json|css|js|woff|woff2|ttf|eot)$/i)) {
|
|
129
|
+
return { included: false, reason: "static-asset" };
|
|
130
|
+
}
|
|
131
|
+
if (respectRobotsTxt && !isPathAllowedByRobots(pathname, robots)) {
|
|
132
|
+
return { included: false, reason: "robots-txt" };
|
|
133
|
+
}
|
|
134
|
+
if (matchesExcludePattern(pathname, excludePatterns)) {
|
|
135
|
+
return { included: false, reason: "exclude-pattern" };
|
|
136
|
+
}
|
|
137
|
+
return { included: true, reason: "included" };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// src/utils/pathUtils.ts
|
|
141
|
+
var import_path2 = require("path");
|
|
142
|
+
var artifactPrefixes = [
|
|
143
|
+
"server/pages/",
|
|
144
|
+
"server/app/",
|
|
145
|
+
"static/chunks/app/",
|
|
146
|
+
"static/chunks/pages/",
|
|
147
|
+
"static/",
|
|
148
|
+
"server/"
|
|
149
|
+
];
|
|
150
|
+
function filePathToPathname(filePath) {
|
|
151
|
+
let pathname = filePath.split(import_path2.sep).join("/");
|
|
152
|
+
for (const prefix of artifactPrefixes) {
|
|
153
|
+
if (pathname.startsWith(prefix)) {
|
|
154
|
+
pathname = pathname.substring(prefix.length);
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
pathname = pathname.replace(/\.html?$/, "");
|
|
159
|
+
if (pathname === "index" || pathname === "") {
|
|
160
|
+
return "/";
|
|
161
|
+
}
|
|
162
|
+
if (pathname.endsWith("/index")) {
|
|
163
|
+
pathname = pathname.slice(0, -5);
|
|
164
|
+
}
|
|
165
|
+
if (!pathname.startsWith("/")) {
|
|
166
|
+
pathname = "/" + pathname;
|
|
167
|
+
}
|
|
168
|
+
return pathname;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// src/parseHtml.ts
|
|
172
|
+
var import_logger = require("@peam-ai/logger");
|
|
173
|
+
var import_jsdom = require("jsdom");
|
|
174
|
+
|
|
175
|
+
// src/parsers/cssSelectorParser.ts
|
|
176
|
+
var CssSelectorParser = class {
|
|
177
|
+
parse(document, options) {
|
|
178
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
179
|
+
const title = (_a = document.querySelector("title")) == null ? void 0 : _a.textContent;
|
|
180
|
+
const description = (_b = document.querySelector('meta[name="description"]')) == null ? void 0 : _b.getAttribute("content");
|
|
181
|
+
const content = document.body.innerHTML;
|
|
182
|
+
const bodyClone = document.body.cloneNode(true);
|
|
183
|
+
bodyClone.querySelectorAll("script, style, noscript, iframe, [hidden], nav, header, footer, aside").forEach((el) => el.remove());
|
|
184
|
+
const textContent = (_c = bodyClone.textContent) == null ? void 0 : _c.replace(/\s+/g, " ").trim();
|
|
185
|
+
if (!title || title.trim().length === 0 || !textContent || textContent.trim().length === 0 || !content || content.trim().length === 0 || !description || description.trim().length === 0) {
|
|
186
|
+
return void 0;
|
|
187
|
+
}
|
|
188
|
+
const page = {
|
|
189
|
+
title,
|
|
190
|
+
description,
|
|
191
|
+
content,
|
|
192
|
+
textContent,
|
|
193
|
+
contentLength: textContent.length
|
|
194
|
+
};
|
|
195
|
+
const htmlLang = document.documentElement.getAttribute("lang");
|
|
196
|
+
if (htmlLang) {
|
|
197
|
+
page.language = htmlLang;
|
|
198
|
+
}
|
|
199
|
+
const dir = document.documentElement.getAttribute("dir");
|
|
200
|
+
if (dir) {
|
|
201
|
+
page.direction = dir;
|
|
202
|
+
}
|
|
203
|
+
const ogTitle = (_d = document.querySelector('meta[property="og:title"]')) == null ? void 0 : _d.getAttribute("content");
|
|
204
|
+
if (ogTitle && ogTitle !== page.title) {
|
|
205
|
+
page.ogTitle = ogTitle;
|
|
206
|
+
}
|
|
207
|
+
const ogDescription = (_e = document.querySelector('meta[property="og:description"]')) == null ? void 0 : _e.getAttribute("content");
|
|
208
|
+
if (ogDescription) {
|
|
209
|
+
page.ogDescription = ogDescription;
|
|
210
|
+
}
|
|
211
|
+
const ogImage = (_f = document.querySelector('meta[property="og:image"]')) == null ? void 0 : _f.getAttribute("content");
|
|
212
|
+
if (ogImage) {
|
|
213
|
+
page.ogImage = ogImage;
|
|
214
|
+
}
|
|
215
|
+
const ogSiteName = (_g = document.querySelector('meta[property="og:site_name"]')) == null ? void 0 : _g.getAttribute("content");
|
|
216
|
+
if (ogSiteName) {
|
|
217
|
+
page.siteName = ogSiteName;
|
|
218
|
+
}
|
|
219
|
+
const author = (_h = document.querySelector('meta[name="author"]')) == null ? void 0 : _h.getAttribute("content");
|
|
220
|
+
if (author) {
|
|
221
|
+
page.author = author;
|
|
222
|
+
}
|
|
223
|
+
const keywords = (_i = document.querySelector('meta[name="keywords"]')) == null ? void 0 : _i.getAttribute("content");
|
|
224
|
+
if (keywords) {
|
|
225
|
+
page.keywords = keywords.split(",").map((k) => k.trim()).filter((k) => k);
|
|
226
|
+
}
|
|
227
|
+
const publishedTime = (_j = document.querySelector('meta[property="article:published_time"]')) == null ? void 0 : _j.getAttribute("content");
|
|
228
|
+
if (publishedTime) {
|
|
229
|
+
page.publishedTime = publishedTime;
|
|
230
|
+
}
|
|
231
|
+
const headings = Array.from(document.querySelectorAll("h1, h2, h3, h4, h5, h6")).map((h) => {
|
|
232
|
+
var _a2;
|
|
233
|
+
return (_a2 = h.textContent) == null ? void 0 : _a2.trim();
|
|
234
|
+
}).filter((h) => !!h && h.length > 0);
|
|
235
|
+
if (headings.length > 0) {
|
|
236
|
+
page.headings = headings;
|
|
237
|
+
}
|
|
238
|
+
if ((options == null ? void 0 : options.extractLinks) !== false) {
|
|
239
|
+
const links = Array.from(document.querySelectorAll("a[href]")).map((a) => a.getAttribute("href")).filter((href) => {
|
|
240
|
+
if (!href) return false;
|
|
241
|
+
return href.startsWith("/") && !href.startsWith("/_next") && !href.startsWith("/_");
|
|
242
|
+
});
|
|
243
|
+
if (links.length > 0) {
|
|
244
|
+
page.internalLinks = Array.from(new Set(links));
|
|
245
|
+
}
|
|
246
|
+
const externalLinks = Array.from(document.querySelectorAll("a[href]")).map((a) => a.getAttribute("href")).filter((href) => {
|
|
247
|
+
if (!href) return false;
|
|
248
|
+
return href.startsWith("http://") || href.startsWith("https://");
|
|
249
|
+
});
|
|
250
|
+
if (externalLinks.length > 0) {
|
|
251
|
+
page.externalLinks = Array.from(new Set(externalLinks));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if ((options == null ? void 0 : options.extractImages) !== false) {
|
|
255
|
+
const images = Array.from(document.querySelectorAll("img[src], img[data-nimg], img[data-src]")).map((img) => {
|
|
256
|
+
const src = img.getAttribute("src") || img.getAttribute("data-src") || img.getAttribute("data-nimg");
|
|
257
|
+
const alt = img.getAttribute("alt");
|
|
258
|
+
return { src, alt };
|
|
259
|
+
}).filter((img) => img.src && !img.src.startsWith("data:"));
|
|
260
|
+
if (images.length > 0) {
|
|
261
|
+
page.images = images.map((img) => img.src || "").filter((src) => src);
|
|
262
|
+
page.imageAlts = images.map((img) => img.alt || "");
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return page;
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
// src/parsers/readabilityParser.ts
|
|
270
|
+
var import_readability = require("@mozilla/readability");
|
|
271
|
+
var ReadabilityParser = class {
|
|
272
|
+
parse(document, options) {
|
|
273
|
+
const { keepClasses, disableJSONLD } = options || {};
|
|
274
|
+
const reader = new import_readability.Readability(document, {
|
|
275
|
+
keepClasses,
|
|
276
|
+
disableJSONLD
|
|
277
|
+
});
|
|
278
|
+
const parsedPage = reader.parse();
|
|
279
|
+
if (parsedPage) {
|
|
280
|
+
const page = {
|
|
281
|
+
title: parsedPage.title,
|
|
282
|
+
description: parsedPage.excerpt,
|
|
283
|
+
content: parsedPage.content,
|
|
284
|
+
textContent: parsedPage.textContent,
|
|
285
|
+
contentLength: parsedPage.length,
|
|
286
|
+
author: parsedPage.byline,
|
|
287
|
+
direction: parsedPage.dir,
|
|
288
|
+
language: parsedPage.lang,
|
|
289
|
+
siteName: parsedPage.siteName,
|
|
290
|
+
publishedTime: parsedPage.publishedTime
|
|
291
|
+
};
|
|
292
|
+
return page;
|
|
293
|
+
}
|
|
294
|
+
return void 0;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// src/parseHtml.ts
|
|
299
|
+
var log = import_logger.loggers.parser;
|
|
300
|
+
function parseHTML(html, options = {}) {
|
|
301
|
+
if (!html || html.trim().length === 0) {
|
|
302
|
+
log.warn("Empty or invalid HTML input");
|
|
303
|
+
return void 0;
|
|
304
|
+
}
|
|
305
|
+
log.debug("Starting parse with options", options);
|
|
306
|
+
const dom = new import_jsdom.JSDOM(html);
|
|
307
|
+
const document = dom.window.document;
|
|
308
|
+
const cssSelectorParser = new CssSelectorParser();
|
|
309
|
+
const cssSelectorStructuredPage = cssSelectorParser.parse(document, options);
|
|
310
|
+
const readabilityParser = new ReadabilityParser();
|
|
311
|
+
const readabilityStructuredPage = readabilityParser.parse(document, options);
|
|
312
|
+
if (!cssSelectorStructuredPage && !readabilityStructuredPage) {
|
|
313
|
+
log.warn("Failed to extract content");
|
|
314
|
+
return void 0;
|
|
315
|
+
}
|
|
316
|
+
return __spreadValues(__spreadValues(__spreadValues({}, {
|
|
317
|
+
title: "",
|
|
318
|
+
description: "",
|
|
319
|
+
content: "",
|
|
320
|
+
textContent: ""
|
|
321
|
+
}), cssSelectorStructuredPage), readabilityStructuredPage);
|
|
322
|
+
}
|
|
323
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
324
|
+
0 && (module.exports = {
|
|
325
|
+
createRobotsParser,
|
|
326
|
+
filePathToPathname,
|
|
327
|
+
isPathAllowedByRobots,
|
|
328
|
+
loadRobotsTxt,
|
|
329
|
+
matchesExcludePattern,
|
|
330
|
+
parseHTML,
|
|
331
|
+
shouldIncludePath
|
|
332
|
+
});
|
|
333
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils/excludePatterns.ts","../src/utils/robotsParser.ts","../src/utils/pathFilter.ts","../src/utils/pathUtils.ts","../src/parseHtml.ts","../src/parsers/cssSelectorParser.ts","../src/parsers/readabilityParser.ts"],"sourcesContent":["export { ParseOptions } from './parsers/parser';\nexport { StructuredPage } from './structuredPage';\n\nexport { matchesExcludePattern } from './utils/excludePatterns';\nexport { shouldIncludePath, type PathFilterReason, type PathFilterResult } from './utils/pathFilter';\nexport { filePathToPathname } from './utils/pathUtils';\nexport {\n createRobotsParser,\n isPathAllowedByRobots,\n loadRobotsTxt,\n type RobotsParser,\n type RobotsTxtResult,\n} from './utils/robotsParser';\n\nexport { parseHTML } from './parseHtml';\n","import { isMatch } from 'matcher';\n\n/**\n * Checks if a path matches any of the given wildcard patterns\n * Uses the matcher library which supports:\n * - * (matches any characters except /)\n * - ** (matches any characters including /)\n * - ? (matches single character)\n * - ! (negation)\n * - [] (character ranges)\n */\nexport function matchesExcludePattern(path: string, patterns: string[]): boolean {\n if (!patterns || patterns.length === 0) {\n return false;\n }\n\n const normalize = (p: string) => (p.startsWith('/') ? p : `/${p}`);\n const normalizedPath = normalize(path);\n const normalizedPatterns = patterns.map(normalize);\n\n return isMatch(normalizedPath, normalizedPatterns);\n}\n","import { existsSync, readFileSync } from 'fs';\nimport { join } from 'path';\nimport robotsParser from 'robots-parser';\n\nexport type RobotsParser = ReturnType<typeof robotsParser>;\n\nexport interface RobotsTxtResult {\n parser: RobotsParser;\n path: string;\n}\n\nexport function createRobotsParser(content: string): RobotsParser {\n return robotsParser('https://robots.invalid/robots.txt', content);\n}\n\n/**\n * Loads and parses robots.txt from custom path or standard locations\n * Returns the parser and the path where robots.txt was found\n */\nexport function loadRobotsTxt(\n projectDir: string,\n searchPaths: string[],\n robotsTxtPath?: string\n): RobotsTxtResult | null {\n let robotsContent: string | null = null;\n let foundPath: string | null = null;\n\n if (robotsTxtPath) {\n const customPath = join(projectDir, robotsTxtPath);\n if (existsSync(customPath)) {\n robotsContent = readFileSync(customPath, 'utf-8');\n foundPath = customPath;\n }\n }\n\n if (!robotsContent) {\n for (const searchPath of searchPaths) {\n const fullPath = join(projectDir, searchPath);\n if (existsSync(fullPath)) {\n robotsContent = readFileSync(fullPath, 'utf-8');\n foundPath = fullPath;\n break;\n }\n }\n }\n\n if (!robotsContent) {\n return null;\n }\n\n return {\n parser: createRobotsParser(robotsContent),\n path: foundPath || '',\n };\n}\n\nexport function isPathAllowedByRobots(path: string, robots: RobotsParser | null): boolean {\n if (!robots) {\n return true;\n }\n\n const normalizedPath = path.startsWith('/') ? path : `/${path}`;\n const testUrl = `https://robots.invalid${normalizedPath}`;\n const isAllowed = robots.isAllowed(testUrl, '*');\n\n return isAllowed !== false;\n}\n","import { matchesExcludePattern } from './excludePatterns';\nimport type { RobotsParser } from './robotsParser';\nimport { isPathAllowedByRobots } from './robotsParser';\n\nexport type PathFilterReason =\n | 'included'\n | 'dynamic-route'\n | 'internal-route'\n | 'rsc-file'\n | 'segment-file'\n | 'static-asset'\n | 'robots-txt'\n | 'exclude-pattern';\n\nexport interface PathFilterResult {\n included: boolean;\n reason: PathFilterReason;\n}\n\n/**\n * Determines if a pathname should be included in the index\n * Returns both the decision and the reason for exclusion\n */\nexport function shouldIncludePath(\n pathname: string,\n robots: RobotsParser | null,\n excludePatterns: string[],\n respectRobotsTxt: boolean\n): PathFilterResult {\n // Exclude routes with dynamic parameters (e.g., /session/[session_id])\n if (pathname.includes('[') && pathname.includes(']')) {\n return { included: false, reason: 'dynamic-route' };\n }\n\n // Exclude Next.js internal routes\n if (pathname.startsWith('/_not-found') || pathname.startsWith('/_global-error')) {\n return { included: false, reason: 'internal-route' };\n }\n\n // Exclude RSC files\n if (pathname.endsWith('.rsc')) {\n return { included: false, reason: 'rsc-file' };\n }\n\n // Exclude segment files\n if (pathname.includes('.segments/')) {\n return { included: false, reason: 'segment-file' };\n }\n\n // Exclude static assets\n if (pathname.match(/\\.(ico|png|jpg|jpeg|svg|gif|webp|txt|xml|json|css|js|woff|woff2|ttf|eot)$/i)) {\n return { included: false, reason: 'static-asset' };\n }\n\n // Check robots.txt rules\n if (respectRobotsTxt && !isPathAllowedByRobots(pathname, robots)) {\n return { included: false, reason: 'robots-txt' };\n }\n\n // Check user-defined exclude patterns\n if (matchesExcludePattern(pathname, excludePatterns)) {\n return { included: false, reason: 'exclude-pattern' };\n }\n\n return { included: true, reason: 'included' };\n}\n","import { sep } from 'path';\n\nconst artifactPrefixes = [\n 'server/pages/',\n 'server/app/',\n 'static/chunks/app/',\n 'static/chunks/pages/',\n 'static/',\n 'server/',\n];\n\n/**\n * Convert file path to URL pathname\n *\n * Examples:\n * index.html -> /\n * about.html -> /about\n * about/index.html -> /about/\n * blog/post-1.html -> /blog/post-1\n * blog/post-1/index.html -> /blog/post-1/\n * server/pages/contact.html -> /contact\n * server/app/about.html -> /about\n */\nexport function filePathToPathname(filePath: string): string {\n let pathname = filePath.split(sep).join('/');\n\n for (const prefix of artifactPrefixes) {\n if (pathname.startsWith(prefix)) {\n pathname = pathname.substring(prefix.length);\n break;\n }\n }\n\n pathname = pathname.replace(/\\.html?$/, '');\n\n if (pathname === 'index' || pathname === '') {\n return '/';\n }\n\n if (pathname.endsWith('/index')) {\n pathname = pathname.slice(0, -5);\n }\n\n if (!pathname.startsWith('/')) {\n pathname = '/' + pathname;\n }\n\n return pathname;\n}\n","import { loggers } from '@peam-ai/logger';\nimport { JSDOM } from 'jsdom';\nimport { CssSelectorParser } from './parsers/cssSelectorParser';\nimport { ParseOptions } from './parsers/parser';\nimport { ReadabilityParser } from './parsers/readabilityParser';\nimport { StructuredPage } from './structuredPage';\n\nconst log = loggers.parser;\n\n/**\n * Parse HTML content and convert it to a StructuredPage\n * @param html - HTML string to parse\n * @param options - Parsing options\n * @returns StructuredPage or undefined if parsing fails\n */\nexport function parseHTML(html: string, options: ParseOptions = {}): StructuredPage | undefined {\n if (!html || html.trim().length === 0) {\n log.warn('Empty or invalid HTML input');\n return undefined;\n }\n\n log.debug('Starting parse with options', options);\n const dom = new JSDOM(html);\n const document = dom.window.document;\n\n const cssSelectorParser = new CssSelectorParser();\n const cssSelectorStructuredPage = cssSelectorParser.parse(document, options);\n const readabilityParser = new ReadabilityParser();\n const readabilityStructuredPage = readabilityParser.parse(document, options);\n\n if (!cssSelectorStructuredPage && !readabilityStructuredPage) {\n log.warn('Failed to extract content');\n return undefined;\n }\n\n // Merge results, prioritizing Readability data\n return {\n ...{\n title: '',\n description: '',\n content: '',\n textContent: '',\n },\n ...cssSelectorStructuredPage,\n ...readabilityStructuredPage,\n };\n}\n","import { StructuredPage } from '../structuredPage';\nimport { ParseOptions, Parser } from './parser';\n\nexport class CssSelectorParser implements Parser {\n parse(document: Document, options?: ParseOptions): StructuredPage | undefined {\n const title = document.querySelector('title')?.textContent;\n const description = document.querySelector('meta[name=\"description\"]')?.getAttribute('content');\n const content = document.body.innerHTML;\n\n const bodyClone = document.body.cloneNode(true) as HTMLElement;\n bodyClone\n .querySelectorAll('script, style, noscript, iframe, [hidden], nav, header, footer, aside')\n .forEach((el) => el.remove());\n\n const textContent = bodyClone.textContent?.replace(/\\s+/g, ' ').trim();\n\n if (\n !title ||\n title.trim().length === 0 ||\n !textContent ||\n textContent.trim().length === 0 ||\n !content ||\n content.trim().length === 0 ||\n !description ||\n description.trim().length === 0\n ) {\n return undefined;\n }\n\n const page: StructuredPage = {\n title,\n description,\n content,\n textContent,\n contentLength: textContent.length,\n };\n\n const htmlLang = document.documentElement.getAttribute('lang');\n if (htmlLang) {\n page.language = htmlLang;\n }\n\n const dir = document.documentElement.getAttribute('dir');\n if (dir) {\n page.direction = dir;\n }\n\n const ogTitle = document.querySelector('meta[property=\"og:title\"]')?.getAttribute('content');\n if (ogTitle && ogTitle !== page.title) {\n page.ogTitle = ogTitle;\n }\n\n const ogDescription = document.querySelector('meta[property=\"og:description\"]')?.getAttribute('content');\n if (ogDescription) {\n page.ogDescription = ogDescription;\n }\n\n const ogImage = document.querySelector('meta[property=\"og:image\"]')?.getAttribute('content');\n if (ogImage) {\n page.ogImage = ogImage;\n }\n\n const ogSiteName = document.querySelector('meta[property=\"og:site_name\"]')?.getAttribute('content');\n if (ogSiteName) {\n page.siteName = ogSiteName;\n }\n\n const author = document.querySelector('meta[name=\"author\"]')?.getAttribute('content');\n if (author) {\n page.author = author;\n }\n\n const keywords = document.querySelector('meta[name=\"keywords\"]')?.getAttribute('content');\n if (keywords) {\n page.keywords = keywords\n .split(',')\n .map((k) => k.trim())\n .filter((k) => k);\n }\n\n const publishedTime = document.querySelector('meta[property=\"article:published_time\"]')?.getAttribute('content');\n if (publishedTime) {\n page.publishedTime = publishedTime;\n }\n\n const headings = Array.from(document.querySelectorAll('h1, h2, h3, h4, h5, h6'))\n .map((h) => h.textContent?.trim())\n .filter((h): h is string => !!h && h.length > 0);\n\n if (headings.length > 0) {\n page.headings = headings;\n }\n\n // Extract links if enabled\n if (options?.extractLinks !== false) {\n // Extract internal links\n const links = Array.from(document.querySelectorAll('a[href]'))\n .map((a) => a.getAttribute('href'))\n .filter((href): href is string => {\n if (!href) return false;\n return href.startsWith('/') && !href.startsWith('/_next') && !href.startsWith('/_');\n });\n\n if (links.length > 0) {\n page.internalLinks = Array.from(new Set(links));\n }\n\n // Extract external links\n const externalLinks = Array.from(document.querySelectorAll('a[href]'))\n .map((a) => a.getAttribute('href'))\n .filter((href): href is string => {\n if (!href) return false;\n return href.startsWith('http://') || href.startsWith('https://');\n });\n\n if (externalLinks.length > 0) {\n page.externalLinks = Array.from(new Set(externalLinks));\n }\n }\n\n // Extract images if enabled\n if (options?.extractImages !== false) {\n const images = Array.from(document.querySelectorAll('img[src], img[data-nimg], img[data-src]'))\n .map((img) => {\n const src = img.getAttribute('src') || img.getAttribute('data-src') || img.getAttribute('data-nimg');\n const alt = img.getAttribute('alt');\n return { src, alt };\n })\n .filter((img) => img.src && !img.src.startsWith('data:'));\n\n if (images.length > 0) {\n page.images = images.map((img) => img.src || '').filter((src) => src);\n page.imageAlts = images.map((img) => img.alt || '');\n }\n }\n\n return page;\n }\n}\n","import { Readability } from '@mozilla/readability';\nimport { StructuredPage } from '../structuredPage';\nimport { ParseOptions, Parser } from './parser';\n\nexport class ReadabilityParser implements Parser {\n parse(document: Document, options?: ParseOptions): StructuredPage | undefined {\n const { keepClasses, disableJSONLD } = options || {};\n\n const reader = new Readability(document, {\n keepClasses,\n disableJSONLD,\n });\n\n const parsedPage = reader.parse();\n\n if (parsedPage) {\n const page: StructuredPage = {\n title: parsedPage.title,\n description: parsedPage.excerpt,\n content: parsedPage.content,\n textContent: parsedPage.textContent,\n contentLength: parsedPage.length,\n author: parsedPage.byline,\n direction: parsedPage.dir,\n language: parsedPage.lang,\n siteName: parsedPage.siteName,\n publishedTime: parsedPage.publishedTime,\n };\n\n return page;\n }\n\n return undefined;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,qBAAwB;AAWjB,SAAS,sBAAsB,MAAc,UAA6B;AAC/E,MAAI,CAAC,YAAY,SAAS,WAAW,GAAG;AACtC,WAAO;AAAA,EACT;AAEA,QAAM,YAAY,CAAC,MAAe,EAAE,WAAW,GAAG,IAAI,IAAI,IAAI,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AM,OAAuB;AAAA,QAC3B,OAAO,WAAW;AAAA,QAClB,aAAa,WAAW;AAAA,QACxB,SAAS,WAAW;AAAA,QACpB,aAAa,WAAW;AAAA,QACxB,eAAe,WAAW;AAAA,QAC1B,QAAQ,WAAW;AAAA,QACnB,WAAW,WAAW;AAAA,QACtB,UAAU,WAAW;AAAA,QACrB,UAAU,WAAW;AAAA,QACrB,eAAe,WAAW;AAAA,MAC5B;AAEA,aAAO;AAAA,IACT;AAEA,WAAO;AAAA,EACT;AACF;;;AF3BA,IAAM,MAAM,sBAAQ;AAQb,SAAS,UAAU,MAAc,UAAwB,CAAC,GAA+B;AAC9F,MAAI,CAAC,QAAQ,KAAK,KAAK,EAAE,WAAW,GAAG;AACrC,QAAI,KAAK,6BAA6B;AACtC,WAAO;AAAA,EACT;AAEA,MAAI,MAAM,+BAA+B,OAAO;AAChD,QAAM,MAAM,IAAI,mBAAM,IAAI;AAC1B,QAAM,WAAW,IAAI,OAAO;AAE5B,QAAM,oBAAoB,IAAI,kBAAkB;AAChD,QAAM,4BAA4B,kBAAkB,MAAM,UAAU,OAAO;AAC3E,QAAM,oBAAoB,IAAI,kBAAkB;AAChD,QAAM,4BAA4B,kBAAkB,MAAM,UAAU,OAAO;AAE3E,MAAI,CAAC,6BAA6B,CAAC,2BAA2B;AAC5D,QAAI,KAAK,2BAA2B;AACpC,WAAO;AAAA,EACT;AAGA,SAAO,iDACF;AAAA,IACD,OAAO;AAAA,IACP,aAAa;AAAA,IACb,SAAS;AAAA,IACT,aAAa;AAAA,EACf,IACG,4BACA;AAEP;","names":["robotsParser","import_path","_a"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/utils/excludePatterns.ts
|
|
19
|
+
import { isMatch } from "matcher";
|
|
20
|
+
function matchesExcludePattern(path, patterns) {
|
|
21
|
+
if (!patterns || patterns.length === 0) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
const normalize = (p) => p.startsWith("/") ? p : `/${p}`;
|
|
25
|
+
const normalizedPath = normalize(path);
|
|
26
|
+
const normalizedPatterns = patterns.map(normalize);
|
|
27
|
+
return isMatch(normalizedPath, normalizedPatterns);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/utils/robotsParser.ts
|
|
31
|
+
import { existsSync, readFileSync } from "fs";
|
|
32
|
+
import { join } from "path";
|
|
33
|
+
import robotsParser from "robots-parser";
|
|
34
|
+
function createRobotsParser(content) {
|
|
35
|
+
return robotsParser("https://robots.invalid/robots.txt", content);
|
|
36
|
+
}
|
|
37
|
+
function loadRobotsTxt(projectDir, searchPaths, robotsTxtPath) {
|
|
38
|
+
let robotsContent = null;
|
|
39
|
+
let foundPath = null;
|
|
40
|
+
if (robotsTxtPath) {
|
|
41
|
+
const customPath = join(projectDir, robotsTxtPath);
|
|
42
|
+
if (existsSync(customPath)) {
|
|
43
|
+
robotsContent = readFileSync(customPath, "utf-8");
|
|
44
|
+
foundPath = customPath;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (!robotsContent) {
|
|
48
|
+
for (const searchPath of searchPaths) {
|
|
49
|
+
const fullPath = join(projectDir, searchPath);
|
|
50
|
+
if (existsSync(fullPath)) {
|
|
51
|
+
robotsContent = readFileSync(fullPath, "utf-8");
|
|
52
|
+
foundPath = fullPath;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (!robotsContent) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
parser: createRobotsParser(robotsContent),
|
|
62
|
+
path: foundPath || ""
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function isPathAllowedByRobots(path, robots) {
|
|
66
|
+
if (!robots) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
70
|
+
const testUrl = `https://robots.invalid${normalizedPath}`;
|
|
71
|
+
const isAllowed = robots.isAllowed(testUrl, "*");
|
|
72
|
+
return isAllowed !== false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/utils/pathFilter.ts
|
|
76
|
+
function shouldIncludePath(pathname, robots, excludePatterns, respectRobotsTxt) {
|
|
77
|
+
if (pathname.includes("[") && pathname.includes("]")) {
|
|
78
|
+
return { included: false, reason: "dynamic-route" };
|
|
79
|
+
}
|
|
80
|
+
if (pathname.startsWith("/_not-found") || pathname.startsWith("/_global-error")) {
|
|
81
|
+
return { included: false, reason: "internal-route" };
|
|
82
|
+
}
|
|
83
|
+
if (pathname.endsWith(".rsc")) {
|
|
84
|
+
return { included: false, reason: "rsc-file" };
|
|
85
|
+
}
|
|
86
|
+
if (pathname.includes(".segments/")) {
|
|
87
|
+
return { included: false, reason: "segment-file" };
|
|
88
|
+
}
|
|
89
|
+
if (pathname.match(/\.(ico|png|jpg|jpeg|svg|gif|webp|txt|xml|json|css|js|woff|woff2|ttf|eot)$/i)) {
|
|
90
|
+
return { included: false, reason: "static-asset" };
|
|
91
|
+
}
|
|
92
|
+
if (respectRobotsTxt && !isPathAllowedByRobots(pathname, robots)) {
|
|
93
|
+
return { included: false, reason: "robots-txt" };
|
|
94
|
+
}
|
|
95
|
+
if (matchesExcludePattern(pathname, excludePatterns)) {
|
|
96
|
+
return { included: false, reason: "exclude-pattern" };
|
|
97
|
+
}
|
|
98
|
+
return { included: true, reason: "included" };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/utils/pathUtils.ts
|
|
102
|
+
import { sep } from "path";
|
|
103
|
+
var artifactPrefixes = [
|
|
104
|
+
"server/pages/",
|
|
105
|
+
"server/app/",
|
|
106
|
+
"static/chunks/app/",
|
|
107
|
+
"static/chunks/pages/",
|
|
108
|
+
"static/",
|
|
109
|
+
"server/"
|
|
110
|
+
];
|
|
111
|
+
function filePathToPathname(filePath) {
|
|
112
|
+
let pathname = filePath.split(sep).join("/");
|
|
113
|
+
for (const prefix of artifactPrefixes) {
|
|
114
|
+
if (pathname.startsWith(prefix)) {
|
|
115
|
+
pathname = pathname.substring(prefix.length);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
pathname = pathname.replace(/\.html?$/, "");
|
|
120
|
+
if (pathname === "index" || pathname === "") {
|
|
121
|
+
return "/";
|
|
122
|
+
}
|
|
123
|
+
if (pathname.endsWith("/index")) {
|
|
124
|
+
pathname = pathname.slice(0, -5);
|
|
125
|
+
}
|
|
126
|
+
if (!pathname.startsWith("/")) {
|
|
127
|
+
pathname = "/" + pathname;
|
|
128
|
+
}
|
|
129
|
+
return pathname;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/parseHtml.ts
|
|
133
|
+
import { loggers } from "@peam-ai/logger";
|
|
134
|
+
import { JSDOM } from "jsdom";
|
|
135
|
+
|
|
136
|
+
// src/parsers/cssSelectorParser.ts
|
|
137
|
+
var CssSelectorParser = class {
|
|
138
|
+
parse(document, options) {
|
|
139
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
140
|
+
const title = (_a = document.querySelector("title")) == null ? void 0 : _a.textContent;
|
|
141
|
+
const description = (_b = document.querySelector('meta[name="description"]')) == null ? void 0 : _b.getAttribute("content");
|
|
142
|
+
const content = document.body.innerHTML;
|
|
143
|
+
const bodyClone = document.body.cloneNode(true);
|
|
144
|
+
bodyClone.querySelectorAll("script, style, noscript, iframe, [hidden], nav, header, footer, aside").forEach((el) => el.remove());
|
|
145
|
+
const textContent = (_c = bodyClone.textContent) == null ? void 0 : _c.replace(/\s+/g, " ").trim();
|
|
146
|
+
if (!title || title.trim().length === 0 || !textContent || textContent.trim().length === 0 || !content || content.trim().length === 0 || !description || description.trim().length === 0) {
|
|
147
|
+
return void 0;
|
|
148
|
+
}
|
|
149
|
+
const page = {
|
|
150
|
+
title,
|
|
151
|
+
description,
|
|
152
|
+
content,
|
|
153
|
+
textContent,
|
|
154
|
+
contentLength: textContent.length
|
|
155
|
+
};
|
|
156
|
+
const htmlLang = document.documentElement.getAttribute("lang");
|
|
157
|
+
if (htmlLang) {
|
|
158
|
+
page.language = htmlLang;
|
|
159
|
+
}
|
|
160
|
+
const dir = document.documentElement.getAttribute("dir");
|
|
161
|
+
if (dir) {
|
|
162
|
+
page.direction = dir;
|
|
163
|
+
}
|
|
164
|
+
const ogTitle = (_d = document.querySelector('meta[property="og:title"]')) == null ? void 0 : _d.getAttribute("content");
|
|
165
|
+
if (ogTitle && ogTitle !== page.title) {
|
|
166
|
+
page.ogTitle = ogTitle;
|
|
167
|
+
}
|
|
168
|
+
const ogDescription = (_e = document.querySelector('meta[property="og:description"]')) == null ? void 0 : _e.getAttribute("content");
|
|
169
|
+
if (ogDescription) {
|
|
170
|
+
page.ogDescription = ogDescription;
|
|
171
|
+
}
|
|
172
|
+
const ogImage = (_f = document.querySelector('meta[property="og:image"]')) == null ? void 0 : _f.getAttribute("content");
|
|
173
|
+
if (ogImage) {
|
|
174
|
+
page.ogImage = ogImage;
|
|
175
|
+
}
|
|
176
|
+
const ogSiteName = (_g = document.querySelector('meta[property="og:site_name"]')) == null ? void 0 : _g.getAttribute("content");
|
|
177
|
+
if (ogSiteName) {
|
|
178
|
+
page.siteName = ogSiteName;
|
|
179
|
+
}
|
|
180
|
+
const author = (_h = document.querySelector('meta[name="author"]')) == null ? void 0 : _h.getAttribute("content");
|
|
181
|
+
if (author) {
|
|
182
|
+
page.author = author;
|
|
183
|
+
}
|
|
184
|
+
const keywords = (_i = document.querySelector('meta[name="keywords"]')) == null ? void 0 : _i.getAttribute("content");
|
|
185
|
+
if (keywords) {
|
|
186
|
+
page.keywords = keywords.split(",").map((k) => k.trim()).filter((k) => k);
|
|
187
|
+
}
|
|
188
|
+
const publishedTime = (_j = document.querySelector('meta[property="article:published_time"]')) == null ? void 0 : _j.getAttribute("content");
|
|
189
|
+
if (publishedTime) {
|
|
190
|
+
page.publishedTime = publishedTime;
|
|
191
|
+
}
|
|
192
|
+
const headings = Array.from(document.querySelectorAll("h1, h2, h3, h4, h5, h6")).map((h) => {
|
|
193
|
+
var _a2;
|
|
194
|
+
return (_a2 = h.textContent) == null ? void 0 : _a2.trim();
|
|
195
|
+
}).filter((h) => !!h && h.length > 0);
|
|
196
|
+
if (headings.length > 0) {
|
|
197
|
+
page.headings = headings;
|
|
198
|
+
}
|
|
199
|
+
if ((options == null ? void 0 : options.extractLinks) !== false) {
|
|
200
|
+
const links = Array.from(document.querySelectorAll("a[href]")).map((a) => a.getAttribute("href")).filter((href) => {
|
|
201
|
+
if (!href) return false;
|
|
202
|
+
return href.startsWith("/") && !href.startsWith("/_next") && !href.startsWith("/_");
|
|
203
|
+
});
|
|
204
|
+
if (links.length > 0) {
|
|
205
|
+
page.internalLinks = Array.from(new Set(links));
|
|
206
|
+
}
|
|
207
|
+
const externalLinks = Array.from(document.querySelectorAll("a[href]")).map((a) => a.getAttribute("href")).filter((href) => {
|
|
208
|
+
if (!href) return false;
|
|
209
|
+
return href.startsWith("http://") || href.startsWith("https://");
|
|
210
|
+
});
|
|
211
|
+
if (externalLinks.length > 0) {
|
|
212
|
+
page.externalLinks = Array.from(new Set(externalLinks));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if ((options == null ? void 0 : options.extractImages) !== false) {
|
|
216
|
+
const images = Array.from(document.querySelectorAll("img[src], img[data-nimg], img[data-src]")).map((img) => {
|
|
217
|
+
const src = img.getAttribute("src") || img.getAttribute("data-src") || img.getAttribute("data-nimg");
|
|
218
|
+
const alt = img.getAttribute("alt");
|
|
219
|
+
return { src, alt };
|
|
220
|
+
}).filter((img) => img.src && !img.src.startsWith("data:"));
|
|
221
|
+
if (images.length > 0) {
|
|
222
|
+
page.images = images.map((img) => img.src || "").filter((src) => src);
|
|
223
|
+
page.imageAlts = images.map((img) => img.alt || "");
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return page;
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// src/parsers/readabilityParser.ts
|
|
231
|
+
import { Readability } from "@mozilla/readability";
|
|
232
|
+
var ReadabilityParser = class {
|
|
233
|
+
parse(document, options) {
|
|
234
|
+
const { keepClasses, disableJSONLD } = options || {};
|
|
235
|
+
const reader = new Readability(document, {
|
|
236
|
+
keepClasses,
|
|
237
|
+
disableJSONLD
|
|
238
|
+
});
|
|
239
|
+
const parsedPage = reader.parse();
|
|
240
|
+
if (parsedPage) {
|
|
241
|
+
const page = {
|
|
242
|
+
title: parsedPage.title,
|
|
243
|
+
description: parsedPage.excerpt,
|
|
244
|
+
content: parsedPage.content,
|
|
245
|
+
textContent: parsedPage.textContent,
|
|
246
|
+
contentLength: parsedPage.length,
|
|
247
|
+
author: parsedPage.byline,
|
|
248
|
+
direction: parsedPage.dir,
|
|
249
|
+
language: parsedPage.lang,
|
|
250
|
+
siteName: parsedPage.siteName,
|
|
251
|
+
publishedTime: parsedPage.publishedTime
|
|
252
|
+
};
|
|
253
|
+
return page;
|
|
254
|
+
}
|
|
255
|
+
return void 0;
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
// src/parseHtml.ts
|
|
260
|
+
var log = loggers.parser;
|
|
261
|
+
function parseHTML(html, options = {}) {
|
|
262
|
+
if (!html || html.trim().length === 0) {
|
|
263
|
+
log.warn("Empty or invalid HTML input");
|
|
264
|
+
return void 0;
|
|
265
|
+
}
|
|
266
|
+
log.debug("Starting parse with options", options);
|
|
267
|
+
const dom = new JSDOM(html);
|
|
268
|
+
const document = dom.window.document;
|
|
269
|
+
const cssSelectorParser = new CssSelectorParser();
|
|
270
|
+
const cssSelectorStructuredPage = cssSelectorParser.parse(document, options);
|
|
271
|
+
const readabilityParser = new ReadabilityParser();
|
|
272
|
+
const readabilityStructuredPage = readabilityParser.parse(document, options);
|
|
273
|
+
if (!cssSelectorStructuredPage && !readabilityStructuredPage) {
|
|
274
|
+
log.warn("Failed to extract content");
|
|
275
|
+
return void 0;
|
|
276
|
+
}
|
|
277
|
+
return __spreadValues(__spreadValues(__spreadValues({}, {
|
|
278
|
+
title: "",
|
|
279
|
+
description: "",
|
|
280
|
+
content: "",
|
|
281
|
+
textContent: ""
|
|
282
|
+
}), cssSelectorStructuredPage), readabilityStructuredPage);
|
|
283
|
+
}
|
|
284
|
+
export {
|
|
285
|
+
createRobotsParser,
|
|
286
|
+
filePathToPathname,
|
|
287
|
+
isPathAllowedByRobots,
|
|
288
|
+
loadRobotsTxt,
|
|
289
|
+
matchesExcludePattern,
|
|
290
|
+
parseHTML,
|
|
291
|
+
shouldIncludePath
|
|
292
|
+
};
|
|
293
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/excludePatterns.ts","../src/utils/robotsParser.ts","../src/utils/pathFilter.ts","../src/utils/pathUtils.ts","../src/parseHtml.ts","../src/parsers/cssSelectorParser.ts","../src/parsers/readabilityParser.ts"],"sourcesContent":["import { isMatch } from 'matcher';\n\n/**\n * Checks if a path matches any of the given wildcard patterns\n * Uses the matcher library which supports:\n * - * (matches any characters except /)\n * - ** (matches any characters including /)\n * - ? (matches single character)\n * - ! (negation)\n * - [] (character ranges)\n */\nexport function matchesExcludePattern(path: string, patterns: string[]): boolean {\n if (!patterns || patterns.length === 0) {\n return false;\n }\n\n const normalize = (p: string) => (p.startsWith('/') ? p : `/${p}`);\n const normalizedPath = normalize(path);\n const normalizedPatterns = patterns.map(normalize);\n\n return isMatch(normalizedPath, normalizedPatterns);\n}\n","import { existsSync, readFileSync } from 'fs';\nimport { join } from 'path';\nimport robotsParser from 'robots-parser';\n\nexport type RobotsParser = ReturnType<typeof robotsParser>;\n\nexport interface RobotsTxtResult {\n parser: RobotsParser;\n path: string;\n}\n\nexport function createRobotsParser(content: string): RobotsParser {\n return robotsParser('https://robots.invalid/robots.txt', content);\n}\n\n/**\n * Loads and parses robots.txt from custom path or standard locations\n * Returns the parser and the path where robots.txt was found\n */\nexport function loadRobotsTxt(\n projectDir: string,\n searchPaths: string[],\n robotsTxtPath?: string\n): RobotsTxtResult | null {\n let robotsContent: string | null = null;\n let foundPath: string | null = null;\n\n if (robotsTxtPath) {\n const customPath = join(projectDir, robotsTxtPath);\n if (existsSync(customPath)) {\n robotsContent = readFileSync(customPath, 'utf-8');\n foundPath = customPath;\n }\n }\n\n if (!robotsContent) {\n for (const searchPath of searchPaths) {\n const fullPath = join(projectDir, searchPath);\n if (existsSync(fullPath)) {\n robotsContent = readFileSync(fullPath, 'utf-8');\n foundPath = fullPath;\n break;\n }\n }\n }\n\n if (!robotsContent) {\n return null;\n }\n\n return {\n parser: createRobotsParser(robotsContent),\n path: foundPath || '',\n };\n}\n\nexport function isPathAllowedByRobots(path: string, robots: RobotsParser | null): boolean {\n if (!robots) {\n return true;\n }\n\n const normalizedPath = path.startsWith('/') ? path : `/${path}`;\n const testUrl = `https://robots.invalid${normalizedPath}`;\n const isAllowed = robots.isAllowed(testUrl, '*');\n\n return isAllowed !== false;\n}\n","import { matchesExcludePattern } from './excludePatterns';\nimport type { RobotsParser } from './robotsParser';\nimport { isPathAllowedByRobots } from './robotsParser';\n\nexport type PathFilterReason =\n | 'included'\n | 'dynamic-route'\n | 'internal-route'\n | 'rsc-file'\n | 'segment-file'\n | 'static-asset'\n | 'robots-txt'\n | 'exclude-pattern';\n\nexport interface PathFilterResult {\n included: boolean;\n reason: PathFilterReason;\n}\n\n/**\n * Determines if a pathname should be included in the index\n * Returns both the decision and the reason for exclusion\n */\nexport function shouldIncludePath(\n pathname: string,\n robots: RobotsParser | null,\n excludePatterns: string[],\n respectRobotsTxt: boolean\n): PathFilterResult {\n // Exclude routes with dynamic parameters (e.g., /session/[session_id])\n if (pathname.includes('[') && pathname.includes(']')) {\n return { included: false, reason: 'dynamic-route' };\n }\n\n // Exclude Next.js internal routes\n if (pathname.startsWith('/_not-found') || pathname.startsWith('/_global-error')) {\n return { included: false, reason: 'internal-route' };\n }\n\n // Exclude RSC files\n if (pathname.endsWith('.rsc')) {\n return { included: false, reason: 'rsc-file' };\n }\n\n // Exclude segment files\n if (pathname.includes('.segments/')) {\n return { included: false, reason: 'segment-file' };\n }\n\n // Exclude static assets\n if (pathname.match(/\\.(ico|png|jpg|jpeg|svg|gif|webp|txt|xml|json|css|js|woff|woff2|ttf|eot)$/i)) {\n return { included: false, reason: 'static-asset' };\n }\n\n // Check robots.txt rules\n if (respectRobotsTxt && !isPathAllowedByRobots(pathname, robots)) {\n return { included: false, reason: 'robots-txt' };\n }\n\n // Check user-defined exclude patterns\n if (matchesExcludePattern(pathname, excludePatterns)) {\n return { included: false, reason: 'exclude-pattern' };\n }\n\n return { included: true, reason: 'included' };\n}\n","import { sep } from 'path';\n\nconst artifactPrefixes = [\n 'server/pages/',\n 'server/app/',\n 'static/chunks/app/',\n 'static/chunks/pages/',\n 'static/',\n 'server/',\n];\n\n/**\n * Convert file path to URL pathname\n *\n * Examples:\n * index.html -> /\n * about.html -> /about\n * about/index.html -> /about/\n * blog/post-1.html -> /blog/post-1\n * blog/post-1/index.html -> /blog/post-1/\n * server/pages/contact.html -> /contact\n * server/app/about.html -> /about\n */\nexport function filePathToPathname(filePath: string): string {\n let pathname = filePath.split(sep).join('/');\n\n for (const prefix of artifactPrefixes) {\n if (pathname.startsWith(prefix)) {\n pathname = pathname.substring(prefix.length);\n break;\n }\n }\n\n pathname = pathname.replace(/\\.html?$/, '');\n\n if (pathname === 'index' || pathname === '') {\n return '/';\n }\n\n if (pathname.endsWith('/index')) {\n pathname = pathname.slice(0, -5);\n }\n\n if (!pathname.startsWith('/')) {\n pathname = '/' + pathname;\n }\n\n return pathname;\n}\n","import { loggers } from '@peam-ai/logger';\nimport { JSDOM } from 'jsdom';\nimport { CssSelectorParser } from './parsers/cssSelectorParser';\nimport { ParseOptions } from './parsers/parser';\nimport { ReadabilityParser } from './parsers/readabilityParser';\nimport { StructuredPage } from './structuredPage';\n\nconst log = loggers.parser;\n\n/**\n * Parse HTML content and convert it to a StructuredPage\n * @param html - HTML string to parse\n * @param options - Parsing options\n * @returns StructuredPage or undefined if parsing fails\n */\nexport function parseHTML(html: string, options: ParseOptions = {}): StructuredPage | undefined {\n if (!html || html.trim().length === 0) {\n log.warn('Empty or invalid HTML input');\n return undefined;\n }\n\n log.debug('Starting parse with options', options);\n const dom = new JSDOM(html);\n const document = dom.window.document;\n\n const cssSelectorParser = new CssSelectorParser();\n const cssSelectorStructuredPage = cssSelectorParser.parse(document, options);\n const readabilityParser = new ReadabilityParser();\n const readabilityStructuredPage = readabilityParser.parse(document, options);\n\n if (!cssSelectorStructuredPage && !readabilityStructuredPage) {\n log.warn('Failed to extract content');\n return undefined;\n }\n\n // Merge results, prioritizing Readability data\n return {\n ...{\n title: '',\n description: '',\n content: '',\n textContent: '',\n },\n ...cssSelectorStructuredPage,\n ...readabilityStructuredPage,\n };\n}\n","import { StructuredPage } from '../structuredPage';\nimport { ParseOptions, Parser } from './parser';\n\nexport class CssSelectorParser implements Parser {\n parse(document: Document, options?: ParseOptions): StructuredPage | undefined {\n const title = document.querySelector('title')?.textContent;\n const description = document.querySelector('meta[name=\"description\"]')?.getAttribute('content');\n const content = document.body.innerHTML;\n\n const bodyClone = document.body.cloneNode(true) as HTMLElement;\n bodyClone\n .querySelectorAll('script, style, noscript, iframe, [hidden], nav, header, footer, aside')\n .forEach((el) => el.remove());\n\n const textContent = bodyClone.textContent?.replace(/\\s+/g, ' ').trim();\n\n if (\n !title ||\n title.trim().length === 0 ||\n !textContent ||\n textContent.trim().length === 0 ||\n !content ||\n content.trim().length === 0 ||\n !description ||\n description.trim().length === 0\n ) {\n return undefined;\n }\n\n const page: StructuredPage = {\n title,\n description,\n content,\n textContent,\n contentLength: textContent.length,\n };\n\n const htmlLang = document.documentElement.getAttribute('lang');\n if (htmlLang) {\n page.language = htmlLang;\n }\n\n const dir = document.documentElement.getAttribute('dir');\n if (dir) {\n page.direction = dir;\n }\n\n const ogTitle = document.querySelector('meta[property=\"og:title\"]')?.getAttribute('content');\n if (ogTitle && ogTitle !== page.title) {\n page.ogTitle = ogTitle;\n }\n\n const ogDescription = document.querySelector('meta[property=\"og:description\"]')?.getAttribute('content');\n if (ogDescription) {\n page.ogDescription = ogDescription;\n }\n\n const ogImage = document.querySelector('meta[property=\"og:image\"]')?.getAttribute('content');\n if (ogImage) {\n page.ogImage = ogImage;\n }\n\n const ogSiteName = document.querySelector('meta[property=\"og:site_name\"]')?.getAttribute('content');\n if (ogSiteName) {\n page.siteName = ogSiteName;\n }\n\n const author = document.querySelector('meta[name=\"author\"]')?.getAttribute('content');\n if (author) {\n page.author = author;\n }\n\n const keywords = document.querySelector('meta[name=\"keywords\"]')?.getAttribute('content');\n if (keywords) {\n page.keywords = keywords\n .split(',')\n .map((k) => k.trim())\n .filter((k) => k);\n }\n\n const publishedTime = document.querySelector('meta[property=\"article:published_time\"]')?.getAttribute('content');\n if (publishedTime) {\n page.publishedTime = publishedTime;\n }\n\n const headings = Array.from(document.querySelectorAll('h1, h2, h3, h4, h5, h6'))\n .map((h) => h.textContent?.trim())\n .filter((h): h is string => !!h && h.length > 0);\n\n if (headings.length > 0) {\n page.headings = headings;\n }\n\n // Extract links if enabled\n if (options?.extractLinks !== false) {\n // Extract internal links\n const links = Array.from(document.querySelectorAll('a[href]'))\n .map((a) => a.getAttribute('href'))\n .filter((href): href is string => {\n if (!href) return false;\n return href.startsWith('/') && !href.startsWith('/_next') && !href.startsWith('/_');\n });\n\n if (links.length > 0) {\n page.internalLinks = Array.from(new Set(links));\n }\n\n // Extract external links\n const externalLinks = Array.from(document.querySelectorAll('a[href]'))\n .map((a) => a.getAttribute('href'))\n .filter((href): href is string => {\n if (!href) return false;\n return href.startsWith('http://') || href.startsWith('https://');\n });\n\n if (externalLinks.length > 0) {\n page.externalLinks = Array.from(new Set(externalLinks));\n }\n }\n\n // Extract images if enabled\n if (options?.extractImages !== false) {\n const images = Array.from(document.querySelectorAll('img[src], img[data-nimg], img[data-src]'))\n .map((img) => {\n const src = img.getAttribute('src') || img.getAttribute('data-src') || img.getAttribute('data-nimg');\n const alt = img.getAttribute('alt');\n return { src, alt };\n })\n .filter((img) => img.src && !img.src.startsWith('data:'));\n\n if (images.length > 0) {\n page.images = images.map((img) => img.src || '').filter((src) => src);\n page.imageAlts = images.map((img) => img.alt || '');\n }\n }\n\n return page;\n }\n}\n","import { Readability } from '@mozilla/readability';\nimport { StructuredPage } from '../structuredPage';\nimport { ParseOptions, Parser } from './parser';\n\nexport class ReadabilityParser implements Parser {\n parse(document: Document, options?: ParseOptions): StructuredPage | undefined {\n const { keepClasses, disableJSONLD } = options || {};\n\n const reader = new Readability(document, {\n keepClasses,\n disableJSONLD,\n });\n\n const parsedPage = reader.parse();\n\n if (parsedPage) {\n const page: StructuredPage = {\n title: parsedPage.title,\n description: parsedPage.excerpt,\n content: parsedPage.content,\n textContent: parsedPage.textContent,\n contentLength: parsedPage.length,\n author: parsedPage.byline,\n direction: parsedPage.dir,\n language: parsedPage.lang,\n siteName: parsedPage.siteName,\n publishedTime: parsedPage.publishedTime,\n };\n\n return page;\n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
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peam-ai/parser",
|
|
3
|
+
"description": "Content parser for extracting page metadata",
|
|
4
|
+
"version": "0.1.1",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"homepage": "https://peam.ai",
|
|
9
|
+
"author": "Afshin Mehrabani",
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/peam-ai/peam.git",
|
|
14
|
+
"directory": "packages/parser"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/peam-ai/peam/issues"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist/**/*"
|
|
21
|
+
],
|
|
22
|
+
"exports": {
|
|
23
|
+
"./package.json": "./package.json",
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.mjs",
|
|
27
|
+
"require": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/jsdom": "^27.0.0",
|
|
32
|
+
"tsup": "^8.2.4",
|
|
33
|
+
"typescript": "^5.5.4"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@mozilla/readability": "^0.5.0",
|
|
37
|
+
"jsdom": "^27.3.0",
|
|
38
|
+
"matcher": "^6.0.0",
|
|
39
|
+
"robots-parser": "^3.0.1",
|
|
40
|
+
"@peam-ai/logger": "0.1.1"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup",
|
|
44
|
+
"clean": "rm -rf dist",
|
|
45
|
+
"test:eslint": "eslint \"src/**/*.ts*\"",
|
|
46
|
+
"test:prettier": "prettier --check \"src/**/*.ts*\"",
|
|
47
|
+
"test:jest": "jest"
|
|
48
|
+
}
|
|
49
|
+
}
|