page-markdown 0.0.0-stage → 0.1.4
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/ADDITIONAL_TERMS.md +122 -0
- package/CHANGELOG.md +124 -0
- package/LICENSE +694 -0
- package/README.md +133 -3
- package/THIRD_PARTY_NOTICES.md +264 -0
- package/package.json +54 -4
- package/robots.txt +79 -0
- package/src/clean.js +111 -0
- package/src/convert.js +219 -0
- package/src/extract.js +70 -0
- package/src/facts.js +96 -0
- package/src/hidden.js +45 -0
- package/src/index.js +162 -0
- package/src/metadata.js +78 -0
- package/src/refine.js +104 -0
- package/src/sanitize.js +193 -0
- package/src/standardize.js +154 -0
package/README.md
CHANGED
|
@@ -1,3 +1,133 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# page-markdown
|
|
2
|
+
|
|
3
|
+
Faithful page-to-Markdown for humans and agents. Give it a web page's DOM and
|
|
4
|
+
it returns clean Markdown plus metadata. It is written for browsers, for
|
|
5
|
+
example an extension's content script, and runs in Node with a DOM library.
|
|
6
|
+
So far it is tested only in Node (see [Known limits](#known-limits-v01)). It
|
|
7
|
+
was built for WATT3D's browser extensions.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
live Document
|
|
11
|
+
→ sanitize inert clone; open shadow DOM inlined within limits; scripts, styles, ads removed
|
|
12
|
+
→ standardize <pre> → fenced code with language; heading wrappers unwrapped; empty and
|
|
13
|
+
javascript: links removed; absolute links and images; lazy images resolved
|
|
14
|
+
→ extract Mozilla Readability on a copy; full-page fallback when it finds too little;
|
|
15
|
+
a dropped lead paragraph restored; a clean title heading
|
|
16
|
+
→ convert Turndown + GitHub-flavoured tables, strikethrough and task lists; never raw HTML
|
|
17
|
+
→ clean fence-safe; keeps short lines and <br> hard breaks; never cuts text
|
|
18
|
+
→ hidden text opt-in (removeHiddenText): tag-character text, variation-selector runs and
|
|
19
|
+
invisible operators removed, fenced code included; counted in omissions
|
|
20
|
+
→ { markdown, metadata, extraction, omissions, warnings, stats }
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The live page is only read, never modified.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm install page-markdown
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import { pageToMarkdown } from "page-markdown";
|
|
35
|
+
|
|
36
|
+
// In a browser (e.g. an extension content script)
|
|
37
|
+
const result = pageToMarkdown(document);
|
|
38
|
+
result.markdown; // "# Title\n\n…"
|
|
39
|
+
result.metadata; // { url, canonicalUrl, title, author, published, siteName, lang, … }
|
|
40
|
+
result.extraction.method; // "readability" | "full_page"
|
|
41
|
+
|
|
42
|
+
// In Node, with any DOM implementation (linkedom shown)
|
|
43
|
+
import { parseHTML } from "linkedom";
|
|
44
|
+
const { document: doc } = parseHTML(html);
|
|
45
|
+
pageToMarkdown(doc, { url: "https://example.com/article" });
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Options
|
|
49
|
+
|
|
50
|
+
| Option | Default | Meaning |
|
|
51
|
+
|---|---|---|
|
|
52
|
+
| `url` | the document's URL | Base for absolute links and images |
|
|
53
|
+
| `extract` | `"readable"` | `"readable"` (Readability, full-page fallback), `"full"`, or a function `(docCopy) => { html, method }` |
|
|
54
|
+
| `minExtractChars` | `100` | Below this, readable extraction falls back to the full page |
|
|
55
|
+
| `includeFullPage` | `false` | Also return `fullPageMarkdown` |
|
|
56
|
+
| `includeTitle` | `true` | Start readable output with `# <headline>`: the page's H1, else its title without the site name |
|
|
57
|
+
| `recoverLead` | `true` | Restore a lead paragraph the extractor dropped, when it matches the meta description |
|
|
58
|
+
| `pageFacts` | `false` | Append `## Page facts`: short label/value pairs outside the article (a sidebar's version or license, spec rows, infobox rows) that the extractor dropped. `extraction.pageFacts` counts them |
|
|
59
|
+
| `removeHiddenText` | `false` | Remove text hidden from people but read by models: runs of tag characters, variation selectors, invisible operators and reserved tag-plane code points, found by [text-sanctifier](https://github.com/iWhatty/text-sanctifier)'s `findHiddenRuns`. Applies to `markdown` (fenced code included), `fullPageMarkdown` and `metadata`. The England, Scotland and Wales flags and a lone emoji or text presentation selector are kept. `omissions` gets one entry per kind (`hidden_tag`, `hidden_variation_selector`, `hidden_invisible_operator`, `hidden_reserved_tag_plane`) with `count` runs and `chars` code points removed from `markdown` |
|
|
60
|
+
| `mode` | `"full"` | `"no_links"` keeps link text; `"text_only"` also drops structure. Fenced code is never changed |
|
|
61
|
+
| `sanitize` | see `DEFAULT_SANITIZE_OPTIONS` | `stripAds`, `extraStripSelectors` (e.g. `["[data-app-overlay]"]`), shadow limits |
|
|
62
|
+
| `readability` | `{}` | Passed to Readability |
|
|
63
|
+
|
|
64
|
+
The stages are also exported for advanced use: `sanitizeToInertDocument`,
|
|
65
|
+
`standardizeDocument`, `extractContent`, `htmlToMarkdown`, `cleanMarkdown`,
|
|
66
|
+
`applyMode`, `collectMetadata`, `collectPageFacts` and `removeHiddenText`.
|
|
67
|
+
|
|
68
|
+
### Recipes
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
// Keep both views: the readable article and the whole page.
|
|
72
|
+
const { markdown, fullPageMarkdown } = pageToMarkdown(document, { includeFullPage: true });
|
|
73
|
+
|
|
74
|
+
// For agents answering questions: keep facts from asides (version, price, specs).
|
|
75
|
+
pageToMarkdown(document, { pageFacts: true });
|
|
76
|
+
|
|
77
|
+
// Drop your own UI before capture.
|
|
78
|
+
pageToMarkdown(document, { sanitize: { extraStripSelectors: ["#my-extension-panel"] } });
|
|
79
|
+
|
|
80
|
+
// Plain text for prompts; code blocks stay intact.
|
|
81
|
+
pageToMarkdown(document, { mode: "text_only" });
|
|
82
|
+
|
|
83
|
+
// Markdown bound for a model: drop hidden instructions, and see what was dropped.
|
|
84
|
+
const { markdown: safe, omissions } = pageToMarkdown(document, { removeHiddenText: true });
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Quality
|
|
88
|
+
|
|
89
|
+
`npm test` runs the contract suite. It covers:
|
|
90
|
+
- golden outputs for 26 fixtures;
|
|
91
|
+
- regressions for known converter bugs, including dropped lead paragraphs,
|
|
92
|
+
raw HTML tables, flattened code in tables, site-suffixed titles and
|
|
93
|
+
unescaped table pipes;
|
|
94
|
+
- modes, metadata, JSON-LD and the full-page fallback;
|
|
95
|
+
- the shadow DOM limits and their omission notice;
|
|
96
|
+
- leaving the live page unmodified.
|
|
97
|
+
|
|
98
|
+
`npm run bench` scores the package against a naive baseline (Turndown over the
|
|
99
|
+
page body). It runs 15 fixture checks and 17 probes on five real pages, which
|
|
100
|
+
`node bench/fetch-pages.mjs` fetches first. See
|
|
101
|
+
[bench/RESULTS.md](bench/RESULTS.md): currently **32/32 against 11/32**.
|
|
102
|
+
|
|
103
|
+
Browser bundle: about 65 KB minified, 23 KB gzip (`npm run size`, ceiling 66 KB
|
|
104
|
+
minified). Readability is most of it.
|
|
105
|
+
|
|
106
|
+
`npm run ci` runs the contract suite and the size check, and must exit 0
|
|
107
|
+
before a commit. It runs locally; the repo deliberately has no cloud CI.
|
|
108
|
+
|
|
109
|
+
## Known limits (v0.1)
|
|
110
|
+
|
|
111
|
+
- **Node only so far.** Tested in Node with linkedom, not yet in a browser. The
|
|
112
|
+
open shadow DOM path is exercised only with a stand-in host in tests.
|
|
113
|
+
- **Some legacy markup loses paragraph breaks.** Readability can drop runs of
|
|
114
|
+
`<br>` between inline elements in old HTML. The text survives; a paragraph
|
|
115
|
+
break may not.
|
|
116
|
+
- **Hidden content stays.** The pipeline reads the static DOM, so elements
|
|
117
|
+
hidden only by CSS remain unless extraction drops them.
|
|
118
|
+
- **`removeHiddenText` covers hidden runs only.** Zero-width spaces, bidi
|
|
119
|
+
controls and look-alike spaces pass through. It also removes runs that are
|
|
120
|
+
legitimate in some scripts: ideographic variation selectors in CJK names and
|
|
121
|
+
the Mongolian free variation selectors.
|
|
122
|
+
- **Iframes and closed shadow roots are not captured.**
|
|
123
|
+
|
|
124
|
+
## Licence
|
|
125
|
+
|
|
126
|
+
page-markdown is under the WATT3D Source License: AGPL-3.0 with the Additional
|
|
127
|
+
Terms in [ADDITIONAL_TERMS.md](ADDITIONAL_TERMS.md) (see [LICENSE](LICENSE)).
|
|
128
|
+
[robots.txt](robots.txt) states the AI-crawler policy.
|
|
129
|
+
|
|
130
|
+
It bundles text-sanctifier's run finder (same licence) and Mozilla Readability
|
|
131
|
+
(Apache-2.0) and Turndown (MIT), whose licences are in
|
|
132
|
+
[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md). Those notices must accompany
|
|
133
|
+
any distribution.
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
page-markdown's own code is under the WATT3D Source License (AGPL-3.0 with the
|
|
4
|
+
Additional Terms in ADDITIONAL_TERMS.md; see LICENSE). It bundles the
|
|
5
|
+
following third-party software, which remains under its own licence terms below.
|
|
6
|
+
These notices must accompany any distribution of page-markdown or a bundle built from it.
|
|
7
|
+
|
|
8
|
+
It also bundles part of text-sanctifier (https://github.com/iWhatty/text-sanctifier),
|
|
9
|
+
which is WATT3D's own and under the same WATT3D Source License, so it needs no
|
|
10
|
+
separate notice.
|
|
11
|
+
|
|
12
|
+
## @mozilla/readability 0.6.0 — Apache License 2.0
|
|
13
|
+
|
|
14
|
+
Source: https://github.com/mozilla/readability
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
Copyright (c) 2010 Arc90 Inc
|
|
18
|
+
|
|
19
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
20
|
+
you may not use this file except in compliance with the License.
|
|
21
|
+
You may obtain a copy of the License at
|
|
22
|
+
|
|
23
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
24
|
+
|
|
25
|
+
Unless required by applicable law or agreed to in writing, software
|
|
26
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
27
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
28
|
+
See the License for the specific language governing permissions and
|
|
29
|
+
limitations under the License.
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## turndown 7.2.4 — MIT
|
|
33
|
+
|
|
34
|
+
Source: https://github.com/mixmark-io/turndown
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
MIT License
|
|
38
|
+
|
|
39
|
+
Copyright (c) 2017 Dom Christie
|
|
40
|
+
|
|
41
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
42
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
43
|
+
in the Software without restriction, including without limitation the rights
|
|
44
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
45
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
46
|
+
furnished to do so, subject to the following conditions:
|
|
47
|
+
|
|
48
|
+
The above copyright notice and this permission notice shall be included in all
|
|
49
|
+
copies or substantial portions of the Software.
|
|
50
|
+
|
|
51
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
52
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
53
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
54
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
55
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
56
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
57
|
+
SOFTWARE.
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Apache License, Version 2.0 (full text, for @mozilla/readability)
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
Apache License
|
|
64
|
+
Version 2.0, January 2004
|
|
65
|
+
http://www.apache.org/licenses/
|
|
66
|
+
|
|
67
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
68
|
+
|
|
69
|
+
1. Definitions.
|
|
70
|
+
|
|
71
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
72
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
73
|
+
|
|
74
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
75
|
+
the copyright owner that is granting the License.
|
|
76
|
+
|
|
77
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
78
|
+
other entities that control, are controlled by, or are under common
|
|
79
|
+
control with that entity. For the purposes of this definition,
|
|
80
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
81
|
+
direction or management of such entity, whether by contract or
|
|
82
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
83
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
84
|
+
|
|
85
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
86
|
+
exercising permissions granted by this License.
|
|
87
|
+
|
|
88
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
89
|
+
including but not limited to software source code, documentation
|
|
90
|
+
source, and configuration files.
|
|
91
|
+
|
|
92
|
+
"Object" form shall mean any form resulting from mechanical
|
|
93
|
+
transformation or translation of a Source form, including but
|
|
94
|
+
not limited to compiled object code, generated documentation,
|
|
95
|
+
and conversions to other media types.
|
|
96
|
+
|
|
97
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
98
|
+
Object form, made available under the License, as indicated by a
|
|
99
|
+
copyright notice that is included in or attached to the work
|
|
100
|
+
(an example is provided in the Appendix below).
|
|
101
|
+
|
|
102
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
103
|
+
form, that is based on (or derived from) the Work and for which the
|
|
104
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
105
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
106
|
+
of this License, Derivative Works shall not include works that remain
|
|
107
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
108
|
+
the Work and Derivative Works thereof.
|
|
109
|
+
|
|
110
|
+
"Contribution" shall mean any work of authorship, including
|
|
111
|
+
the original version of the Work and any modifications or additions
|
|
112
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
113
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
114
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
115
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
116
|
+
means any form of electronic, verbal, or written communication sent
|
|
117
|
+
to the Licensor or its representatives, including but not limited to
|
|
118
|
+
communication on electronic mailing lists, source code control systems,
|
|
119
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
120
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
121
|
+
excluding communication that is conspicuously marked or otherwise
|
|
122
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
123
|
+
|
|
124
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
125
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
126
|
+
subsequently incorporated within the Work.
|
|
127
|
+
|
|
128
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
129
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
130
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
131
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
132
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
133
|
+
Work and such Derivative Works in Source or Object form.
|
|
134
|
+
|
|
135
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
136
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
137
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
138
|
+
(except as stated in this section) patent license to make, have made,
|
|
139
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
140
|
+
where such license applies only to those patent claims licensable
|
|
141
|
+
by such Contributor that are necessarily infringed by their
|
|
142
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
143
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
144
|
+
institute patent litigation against any entity (including a
|
|
145
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
146
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
147
|
+
or contributory patent infringement, then any patent licenses
|
|
148
|
+
granted to You under this License for that Work shall terminate
|
|
149
|
+
as of the date such litigation is filed.
|
|
150
|
+
|
|
151
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
152
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
153
|
+
modifications, and in Source or Object form, provided that You
|
|
154
|
+
meet the following conditions:
|
|
155
|
+
|
|
156
|
+
(a) You must give any other recipients of the Work or
|
|
157
|
+
Derivative Works a copy of this License; and
|
|
158
|
+
|
|
159
|
+
(b) You must cause any modified files to carry prominent notices
|
|
160
|
+
stating that You changed the files; and
|
|
161
|
+
|
|
162
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
163
|
+
that You distribute, all copyright, patent, trademark, and
|
|
164
|
+
attribution notices from the Source form of the Work,
|
|
165
|
+
excluding those notices that do not pertain to any part of
|
|
166
|
+
the Derivative Works; and
|
|
167
|
+
|
|
168
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
169
|
+
distribution, then any Derivative Works that You distribute must
|
|
170
|
+
include a readable copy of the attribution notices contained
|
|
171
|
+
within such NOTICE file, excluding those notices that do not
|
|
172
|
+
pertain to any part of the Derivative Works, in at least one
|
|
173
|
+
of the following places: within a NOTICE text file distributed
|
|
174
|
+
as part of the Derivative Works; within the Source form or
|
|
175
|
+
documentation, if provided along with the Derivative Works; or,
|
|
176
|
+
within a display generated by the Derivative Works, if and
|
|
177
|
+
wherever such third-party notices normally appear. The contents
|
|
178
|
+
of the NOTICE file are for informational purposes only and
|
|
179
|
+
do not modify the License. You may add Your own attribution
|
|
180
|
+
notices within Derivative Works that You distribute, alongside
|
|
181
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
182
|
+
that such additional attribution notices cannot be construed
|
|
183
|
+
as modifying the License.
|
|
184
|
+
|
|
185
|
+
You may add Your own copyright statement to Your modifications and
|
|
186
|
+
may provide additional or different license terms and conditions
|
|
187
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
188
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
189
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
190
|
+
the conditions stated in this License.
|
|
191
|
+
|
|
192
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
193
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
194
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
195
|
+
this License, without any additional terms or conditions.
|
|
196
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
197
|
+
the terms of any separate license agreement you may have executed
|
|
198
|
+
with Licensor regarding such Contributions.
|
|
199
|
+
|
|
200
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
201
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
202
|
+
except as required for reasonable and customary use in describing the
|
|
203
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
204
|
+
|
|
205
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
206
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
207
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
208
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
209
|
+
implied, including, without limitation, any warranties or conditions
|
|
210
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
211
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
212
|
+
appropriateness of using or redistributing the Work and assume any
|
|
213
|
+
risks associated with Your exercise of permissions under this License.
|
|
214
|
+
|
|
215
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
216
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
217
|
+
unless required by applicable law (such as deliberate and grossly
|
|
218
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
219
|
+
liable to You for damages, including any direct, indirect, special,
|
|
220
|
+
incidental, or consequential damages of any character arising as a
|
|
221
|
+
result of this License or out of the use or inability to use the
|
|
222
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
223
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
224
|
+
other commercial damages or losses), even if such Contributor
|
|
225
|
+
has been advised of the possibility of such damages.
|
|
226
|
+
|
|
227
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
228
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
229
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
230
|
+
or other liability obligations and/or rights consistent with this
|
|
231
|
+
License. However, in accepting such obligations, You may act only
|
|
232
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
233
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
234
|
+
defend, and hold each Contributor harmless for any liability
|
|
235
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
236
|
+
of your accepting any such warranty or additional liability.
|
|
237
|
+
|
|
238
|
+
END OF TERMS AND CONDITIONS
|
|
239
|
+
|
|
240
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
241
|
+
|
|
242
|
+
To apply the Apache License to your work, attach the following
|
|
243
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
244
|
+
replaced with your own identifying information. (Don't include
|
|
245
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
246
|
+
comment syntax for the file format. We also recommend that a
|
|
247
|
+
file or class name and description of purpose be included on the
|
|
248
|
+
same "printed page" as the copyright notice for easier
|
|
249
|
+
identification within third-party archives.
|
|
250
|
+
|
|
251
|
+
Copyright [yyyy] [name of copyright owner]
|
|
252
|
+
|
|
253
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
254
|
+
you may not use this file except in compliance with the License.
|
|
255
|
+
You may obtain a copy of the License at
|
|
256
|
+
|
|
257
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
258
|
+
|
|
259
|
+
Unless required by applicable law or agreed to in writing, software
|
|
260
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
261
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
262
|
+
See the License for the specific language governing permissions and
|
|
263
|
+
limitations under the License.
|
|
264
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "page-markdown",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"description": "Faithful page-to-Markdown for humans and agents: sanitize, extract the readable article, convert with GFM, and clean without losing content.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"src",
|
|
11
|
+
"LICENSE",
|
|
12
|
+
"ADDITIONAL_TERMS.md",
|
|
13
|
+
"THIRD_PARTY_NOTICES.md",
|
|
14
|
+
"README.md",
|
|
15
|
+
"CHANGELOG.md",
|
|
16
|
+
"robots.txt"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"packageManager": "pnpm@10.33.0",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=20"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "node --test \"test/**/*.test.mjs\"",
|
|
25
|
+
"size": "node bench/size.mjs",
|
|
26
|
+
"ci": "npm test && npm run size",
|
|
27
|
+
"bench": "node bench/compare.mjs"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"markdown",
|
|
31
|
+
"readability",
|
|
32
|
+
"html-to-markdown",
|
|
33
|
+
"reader-mode",
|
|
34
|
+
"web-clipper",
|
|
35
|
+
"browser-extension"
|
|
36
|
+
],
|
|
37
|
+
"author": "WATT3D <WATT3D@protonmail.com>",
|
|
38
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/iWhatty/page-markdown.git"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/iWhatty/page-markdown#readme",
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/iWhatty/page-markdown/issues"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@mozilla/readability": "0.6.0",
|
|
49
|
+
"text-sanctifier": "1.3.8",
|
|
50
|
+
"turndown": "7.2.4"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"esbuild": "0.27.0",
|
|
54
|
+
"linkedom": "0.18.12"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/robots.txt
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# WATT3D — AI scraping policy
|
|
2
|
+
#
|
|
3
|
+
# This file enumerates known AI / LLM training and inference crawlers
|
|
4
|
+
# that the WATT3D AI Training Rider does not consent to. See
|
|
5
|
+
# ./ADDITIONAL_TERMS.md for the legal scope and definitions.
|
|
6
|
+
#
|
|
7
|
+
# Honoring robots.txt is voluntary. The rider applies whether or not
|
|
8
|
+
# a bot respects this file. The enumeration is non-exhaustive — bots
|
|
9
|
+
# not listed here are still bound by the rider if they perform
|
|
10
|
+
# Training Use as defined in ADDITIONAL_TERMS.md §1.
|
|
11
|
+
|
|
12
|
+
User-agent: GPTBot
|
|
13
|
+
Disallow: /
|
|
14
|
+
|
|
15
|
+
User-agent: ChatGPT-User
|
|
16
|
+
Disallow: /
|
|
17
|
+
|
|
18
|
+
User-agent: OAI-SearchBot
|
|
19
|
+
Disallow: /
|
|
20
|
+
|
|
21
|
+
User-agent: anthropic-ai
|
|
22
|
+
Disallow: /
|
|
23
|
+
|
|
24
|
+
User-agent: ClaudeBot
|
|
25
|
+
Disallow: /
|
|
26
|
+
|
|
27
|
+
User-agent: Claude-Web
|
|
28
|
+
Disallow: /
|
|
29
|
+
|
|
30
|
+
User-agent: Claude-SearchBot
|
|
31
|
+
Disallow: /
|
|
32
|
+
|
|
33
|
+
User-agent: Google-Extended
|
|
34
|
+
Disallow: /
|
|
35
|
+
|
|
36
|
+
User-agent: Applebot-Extended
|
|
37
|
+
Disallow: /
|
|
38
|
+
|
|
39
|
+
User-agent: Meta-ExternalAgent
|
|
40
|
+
Disallow: /
|
|
41
|
+
|
|
42
|
+
User-agent: Meta-ExternalFetcher
|
|
43
|
+
Disallow: /
|
|
44
|
+
|
|
45
|
+
User-agent: FacebookBot
|
|
46
|
+
Disallow: /
|
|
47
|
+
|
|
48
|
+
User-agent: CCBot
|
|
49
|
+
Disallow: /
|
|
50
|
+
|
|
51
|
+
User-agent: PerplexityBot
|
|
52
|
+
Disallow: /
|
|
53
|
+
|
|
54
|
+
User-agent: Perplexity-User
|
|
55
|
+
Disallow: /
|
|
56
|
+
|
|
57
|
+
User-agent: cohere-ai
|
|
58
|
+
Disallow: /
|
|
59
|
+
|
|
60
|
+
User-agent: Bytespider
|
|
61
|
+
Disallow: /
|
|
62
|
+
|
|
63
|
+
User-agent: Amazonbot
|
|
64
|
+
Disallow: /
|
|
65
|
+
|
|
66
|
+
User-agent: Omgilibot
|
|
67
|
+
Disallow: /
|
|
68
|
+
|
|
69
|
+
User-agent: AI2Bot
|
|
70
|
+
Disallow: /
|
|
71
|
+
|
|
72
|
+
User-agent: Diffbot
|
|
73
|
+
Disallow: /
|
|
74
|
+
|
|
75
|
+
# Everyone else: full access. Search engines and human-driven crawlers
|
|
76
|
+
# remain welcome.
|
|
77
|
+
|
|
78
|
+
User-agent: *
|
|
79
|
+
Allow: /
|
package/src/clean.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Stage 5: tidy Markdown without losing content. Code fences are never
|
|
2
|
+
// touched. Short lines (table cells, list items, numbers) are kept, and no
|
|
3
|
+
// regex spans lines, so text is never cut.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Applies `transform` to the prose between fenced code blocks only.
|
|
7
|
+
* A shorter embedded fence does not close a block; an unterminated fence
|
|
8
|
+
* keeps the rest verbatim.
|
|
9
|
+
* @param {string} markdown
|
|
10
|
+
* @param {(prose: string) => string} transform
|
|
11
|
+
*/
|
|
12
|
+
export function mapOutsideFences(markdown, transform) {
|
|
13
|
+
const blocks = [];
|
|
14
|
+
let lines = [];
|
|
15
|
+
let fence = null;
|
|
16
|
+
const flush = (isCode) => {
|
|
17
|
+
if (!lines.length) return;
|
|
18
|
+
const text = lines.join("\n");
|
|
19
|
+
blocks.push(isCode ? text : transform(text));
|
|
20
|
+
lines = [];
|
|
21
|
+
};
|
|
22
|
+
for (const line of String(markdown).split("\n")) {
|
|
23
|
+
const match = line.match(/^ {0,3}(`{3,}|~{3,})(.*)$/);
|
|
24
|
+
if (!fence && match && !(match[1][0] === "`" && match[2].includes("`"))) {
|
|
25
|
+
flush(false);
|
|
26
|
+
fence = match[1];
|
|
27
|
+
lines.push(line);
|
|
28
|
+
} else if (fence && match && match[1][0] === fence[0] && match[1].length >= fence.length && !match[2].trim()) {
|
|
29
|
+
lines.push(line);
|
|
30
|
+
flush(true);
|
|
31
|
+
fence = null;
|
|
32
|
+
} else {
|
|
33
|
+
lines.push(line);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
flush(Boolean(fence));
|
|
37
|
+
return blocks.join("\n");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// A single prose line holding a large, valid JSON document (embedded app
|
|
41
|
+
// state). Bounded to one line and verified by parsing, so text is never cut.
|
|
42
|
+
const MIN_JSON_LINE = 2000;
|
|
43
|
+
|
|
44
|
+
function isJsonLine(line) {
|
|
45
|
+
const s = line.trim();
|
|
46
|
+
if (s.length < MIN_JSON_LINE || !/^[[{]/.test(s)) return false;
|
|
47
|
+
try {
|
|
48
|
+
JSON.parse(s);
|
|
49
|
+
return true;
|
|
50
|
+
} catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {string} markdown
|
|
57
|
+
* @returns {{ markdown: string, removedJsonLines: number }}
|
|
58
|
+
*/
|
|
59
|
+
export function cleanMarkdown(markdown) {
|
|
60
|
+
let removedJsonLines = 0;
|
|
61
|
+
const cleaned = mapOutsideFences(markdown, (prose) => prose
|
|
62
|
+
.split("\n")
|
|
63
|
+
.filter((line) => {
|
|
64
|
+
if (isJsonLine(line)) {
|
|
65
|
+
removedJsonLines += 1;
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
})
|
|
70
|
+
// Trim trailing whitespace, but keep a two-space hard break (<br>) on a
|
|
71
|
+
// line with text; whitespace-only lines become empty.
|
|
72
|
+
.map((line) => (/\S {2,}$/.test(line) ? line.replace(/[ \t]+$/, " ") : line.replace(/[ \t]+$/, "")))
|
|
73
|
+
.join("\n")
|
|
74
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
75
|
+
// Blocks are joined with "\n": one blank line at most beside a fence.
|
|
76
|
+
.replace(/^\n+/, "\n")
|
|
77
|
+
.replace(/\n+$/, "\n"));
|
|
78
|
+
return { markdown: cleaned.replace(/^\n+|\n+$/g, ""), removedJsonLines };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Link text may hold escaped brackets and whole images: [](href)
|
|
82
|
+
// keeps the image, not "![alt" pointing at href.
|
|
83
|
+
const LINK_TARGET = String.raw`\((?:[^()\s]|\([^()]*\))+(?:\s+"[^"]*")?\)`;
|
|
84
|
+
const INLINE_LINK = new RegExp(String.raw`(!?)\[((?:\\.|!\[[^\]]*\]${LINK_TARGET}|[^\[\]\\])*)\]${LINK_TARGET}`, "g");
|
|
85
|
+
const stripInlineLinks = (md) => md.replace(INLINE_LINK, (all, bang, text) => (bang ? all : text));
|
|
86
|
+
const dropReferenceDefs = (md) => md.replace(/^\s{0,3}\[[^\]]+]:\s+\S+.*$/gm, "");
|
|
87
|
+
const stripStructure = (md) => md
|
|
88
|
+
.replace(/^\s{0,3}#{1,6}\s+/gm, "")
|
|
89
|
+
.replace(/^\s*[-+*]\s+/gm, "")
|
|
90
|
+
.replace(/^\s*\d+\.\s+/gm, "")
|
|
91
|
+
.replace(/^\s{0,3}>\s?/gm, "")
|
|
92
|
+
.replace(/^\s*([-_*])\s*(?:\1\s*){2,}$/gm, "");
|
|
93
|
+
const stripEmphasis = (md) => md.replace(/(\*\*|__)(.+?)\1/g, "$2").replace(/(?<![\w*])([*_])(?!\s)(.+?)(?<!\s)\1(?![\w*])/g, "$2");
|
|
94
|
+
const stripImages = (md) => md.replace(/!\[([^\]]*)\]\([^)]*\)/g, "$1");
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Output modes. "no_links" keeps link text. "text_only" also drops headings,
|
|
98
|
+
* list markers, emphasis and images. Both leave fenced code verbatim.
|
|
99
|
+
* @param {string} markdown
|
|
100
|
+
* @param {"full"|"no_links"|"text_only"} mode
|
|
101
|
+
*/
|
|
102
|
+
export function applyMode(markdown, mode = "full") {
|
|
103
|
+
if (mode === "no_links") return mapOutsideFences(markdown, stripInlineLinks);
|
|
104
|
+
if (mode === "text_only") {
|
|
105
|
+
// Blank-line collapsing stays inside the prose transform: code keeps its own.
|
|
106
|
+
return mapOutsideFences(markdown, (prose) => stripEmphasis(stripStructure(stripImages(dropReferenceDefs(stripInlineLinks(prose)))))
|
|
107
|
+
.replace(/\n{3,}/g, "\n\n"))
|
|
108
|
+
.trim();
|
|
109
|
+
}
|
|
110
|
+
return markdown;
|
|
111
|
+
}
|