@weborigami/origami 0.3.1 → 0.3.3-jse.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/main.js +0 -1
- package/package.json +6 -4
- package/src/builtins.js +0 -2
- package/src/builtinsNew.js +84 -0
- package/src/common/documentObject.js +7 -0
- package/src/dev/crawler/audit.js +85 -0
- package/src/{site → dev}/crawler/crawl.js +3 -66
- package/src/{site → dev}/crawler/crawlResources.js +44 -18
- package/src/dev/crawler/findPaths.js +90 -0
- package/src/dev/crawler/pathsInCss.js +51 -0
- package/src/dev/crawler/pathsInHtml.js +161 -0
- package/src/dev/crawler/pathsInImageMap.js +25 -0
- package/src/dev/crawler/pathsInJs.js +140 -0
- package/src/dev/crawler/pathsInRobotsTxt.js +20 -0
- package/src/dev/crawler/pathsInSitemap.js +20 -0
- package/src/dev/crawler/utilities.js +125 -0
- package/src/dev/dev.js +2 -0
- package/src/handlers/handlers.js +7 -0
- package/src/handlers/jse.handler.js +16 -0
- package/src/handlers/jseModeParent.js +30 -0
- package/src/handlers/jsedocument.handler.js +16 -0
- package/src/handlers/ori.handler.js +2 -1
- package/src/handlers/oridocument.handler.js +2 -1
- package/src/handlers/ts.handler.js +1 -0
- package/src/handlers/txt.handler.js +9 -1
- package/src/help/help.yaml +6 -6
- package/src/internal.js +4 -0
- package/src/js.js +52 -11
- package/src/site/site.js +0 -2
- package/src/text/htmlDom.js +6 -0
- package/src/text/text.js +1 -0
- package/src/calc/calc.js +0 -81
- package/src/site/audit.js +0 -19
- package/src/site/crawler/findPaths.js +0 -266
- package/src/site/crawler/utilities.js +0 -37
package/main.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weborigami/origami",
|
|
3
|
-
"version": "0.3.1",
|
|
3
|
+
"version": "0.3.3-jse.1",
|
|
4
4
|
"description": "Web Origami language, CLI, framework, and server",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -17,13 +17,15 @@
|
|
|
17
17
|
"typescript": "5.8.2"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@weborigami/async-tree": "0.3.1",
|
|
21
|
-
"@weborigami/language": "0.3.1",
|
|
20
|
+
"@weborigami/async-tree": "0.3.3-jse.1",
|
|
22
21
|
"@weborigami/json-feed-to-rss": "1.0.0",
|
|
23
|
-
"@weborigami/
|
|
22
|
+
"@weborigami/language": "0.3.3-jse.1",
|
|
23
|
+
"@weborigami/types": "0.3.3-jse.1",
|
|
24
|
+
"css-tree": "3.1.0",
|
|
24
25
|
"exif-parser": "0.1.12",
|
|
25
26
|
"graphviz-wasm": "3.0.2",
|
|
26
27
|
"highlight.js": "11.11.1",
|
|
28
|
+
"jsdom": "26.1.0",
|
|
27
29
|
"marked": "15.0.7",
|
|
28
30
|
"marked-gfm-heading-id": "4.1.1",
|
|
29
31
|
"marked-highlight": "2.2.1",
|
package/src/builtins.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as calc from "./calc/calc.js";
|
|
2
1
|
import * as dev from "./dev/dev.js";
|
|
3
2
|
import * as handlers from "./handlers/handlers.js";
|
|
4
3
|
import help from "./help/help.js";
|
|
@@ -22,7 +21,6 @@ import * as tree from "./tree/tree.js";
|
|
|
22
21
|
|
|
23
22
|
/** @type {any} */
|
|
24
23
|
export default {
|
|
25
|
-
"calc:": adjustReservedWords(calc),
|
|
26
24
|
"dev:": dev,
|
|
27
25
|
"explore:": explore,
|
|
28
26
|
"files:": files,
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ObjectTree,
|
|
3
|
+
trailingSlash,
|
|
4
|
+
text as treeText,
|
|
5
|
+
} from "@weborigami/async-tree";
|
|
6
|
+
import * as dev from "./dev/dev.js";
|
|
7
|
+
import * as handlers from "./handlers/handlers.js";
|
|
8
|
+
import help from "./help/help.js";
|
|
9
|
+
import * as image from "./image/image.js";
|
|
10
|
+
import js from "./js.js";
|
|
11
|
+
import node from "./node.js";
|
|
12
|
+
import * as origami from "./origami/origami.js";
|
|
13
|
+
import explore from "./protocols/explore.js";
|
|
14
|
+
import files from "./protocols/files.js";
|
|
15
|
+
import http from "./protocols/http.js";
|
|
16
|
+
import https from "./protocols/https.js";
|
|
17
|
+
import httpstree from "./protocols/httpstree.js";
|
|
18
|
+
import httptree from "./protocols/httptree.js";
|
|
19
|
+
import inherited from "./protocols/inherited.js";
|
|
20
|
+
import instantiate from "./protocols/new.js";
|
|
21
|
+
import packageNamespace from "./protocols/package.js";
|
|
22
|
+
import scope from "./protocols/scope.js";
|
|
23
|
+
import * as site from "./site/site.js";
|
|
24
|
+
import * as text from "./text/text.js";
|
|
25
|
+
import * as tree from "./tree/tree.js";
|
|
26
|
+
|
|
27
|
+
// See notes in builtinsTree.js
|
|
28
|
+
class BuiltinsTree {
|
|
29
|
+
constructor(object) {
|
|
30
|
+
this.object = object;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async get(key) {
|
|
34
|
+
const normalizedKey = trailingSlash.remove(key);
|
|
35
|
+
return this.object[normalizedKey];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async keys() {
|
|
39
|
+
return Object.keys(this.object);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const Tree = new BuiltinsTree({
|
|
44
|
+
...tree,
|
|
45
|
+
indent: text.indent,
|
|
46
|
+
json: origami.json,
|
|
47
|
+
text: treeText,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const Origami = new BuiltinsTree({
|
|
51
|
+
...dev,
|
|
52
|
+
...origami,
|
|
53
|
+
...site,
|
|
54
|
+
...text,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const Image = new BuiltinsTree({
|
|
58
|
+
...image,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
/** @type {any} */
|
|
62
|
+
export default new ObjectTree({
|
|
63
|
+
"explore:": explore,
|
|
64
|
+
"files:": files,
|
|
65
|
+
"help:": help,
|
|
66
|
+
"http:": http,
|
|
67
|
+
"https:": https,
|
|
68
|
+
"httpstree:": httpstree,
|
|
69
|
+
"httptree:": httptree,
|
|
70
|
+
"inherited:": inherited,
|
|
71
|
+
"new:": instantiate,
|
|
72
|
+
"node:": node,
|
|
73
|
+
"package:": packageNamespace,
|
|
74
|
+
"scope:": scope,
|
|
75
|
+
|
|
76
|
+
...js,
|
|
77
|
+
|
|
78
|
+
Tree,
|
|
79
|
+
Origami,
|
|
80
|
+
Image,
|
|
81
|
+
|
|
82
|
+
// Some builtins need to be exposed at top level
|
|
83
|
+
...handlers.default,
|
|
84
|
+
});
|
|
@@ -37,6 +37,13 @@ export default async function documentObject(input, data) {
|
|
|
37
37
|
// };
|
|
38
38
|
// const result = Object.create(base);
|
|
39
39
|
const result = {};
|
|
40
|
+
// TODO: Deprecate @text
|
|
40
41
|
Object.assign(result, inputData, data, { "@text": text });
|
|
42
|
+
Object.defineProperty(result, "_body", {
|
|
43
|
+
configurable: true,
|
|
44
|
+
value: text,
|
|
45
|
+
enumerable: false, // TODO: Make enumerable
|
|
46
|
+
writable: true,
|
|
47
|
+
});
|
|
41
48
|
return result;
|
|
42
49
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { pathFromKeys, symbols, Tree } from "@weborigami/async-tree";
|
|
2
|
+
import getTreeArgument from "../../common/getTreeArgument.js";
|
|
3
|
+
import crawlResources from "./crawlResources.js";
|
|
4
|
+
import { getBaseUrl } from "./utilities.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Crawl the indicated tree and return an audit of any broken links to internal
|
|
8
|
+
* pages or other resources.
|
|
9
|
+
*
|
|
10
|
+
* @typedef {import("@weborigami/types").AsyncTree} AsyncTree
|
|
11
|
+
* @typedef {import("@weborigami/async-tree").Treelike} Treelike
|
|
12
|
+
*
|
|
13
|
+
* @this {AsyncTree|null}
|
|
14
|
+
* @param {Treelike} treelike
|
|
15
|
+
* @param {string} [baseHref]
|
|
16
|
+
*/
|
|
17
|
+
export default async function audit(treelike, baseHref) {
|
|
18
|
+
const tree = await getTreeArgument(this, arguments, treelike, "site:audit");
|
|
19
|
+
const baseUrl = getBaseUrl(baseHref, treelike);
|
|
20
|
+
|
|
21
|
+
let errors = {};
|
|
22
|
+
let report;
|
|
23
|
+
const resourceReferences = {};
|
|
24
|
+
const resourcePromises = {};
|
|
25
|
+
|
|
26
|
+
// Iterate through all the resources to crawl the whole tree.
|
|
27
|
+
for await (const result of crawlResources(tree, baseUrl)) {
|
|
28
|
+
const { normalizedKeys, resourcePaths, value: resource } = result;
|
|
29
|
+
const normalizedPath = pathFromKeys(normalizedKeys);
|
|
30
|
+
if (normalizedPath === "crawl-errors.json") {
|
|
31
|
+
// Final error report; add missing pages to the errors
|
|
32
|
+
report = JSON.parse(resource);
|
|
33
|
+
for (const [path, pagePaths] of Object.entries(report)) {
|
|
34
|
+
if (!errors[path]) {
|
|
35
|
+
errors[path] = [];
|
|
36
|
+
}
|
|
37
|
+
errors[path].push(...pagePaths);
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
// Record which resources this path references
|
|
41
|
+
resourceReferences[normalizedPath] = resourcePaths;
|
|
42
|
+
|
|
43
|
+
// Add all resources to the set that should be verified
|
|
44
|
+
for (const resourcePath of resourcePaths) {
|
|
45
|
+
// Start request, don't wait for it to complete yet
|
|
46
|
+
resourcePromises[resourcePath] ??= Tree.traversePath(
|
|
47
|
+
tree,
|
|
48
|
+
resourcePath
|
|
49
|
+
).then(
|
|
50
|
+
// Just return true or false to indicate if value is defined
|
|
51
|
+
(value) => value !== undefined
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Add any references to missing resources to the errors
|
|
58
|
+
for (const [refererPath, resourcePaths] of Object.entries(
|
|
59
|
+
resourceReferences
|
|
60
|
+
)) {
|
|
61
|
+
for (const resourcePath of resourcePaths) {
|
|
62
|
+
const found = await resourcePromises[resourcePath];
|
|
63
|
+
if (!found) {
|
|
64
|
+
if (!errors[refererPath]) {
|
|
65
|
+
errors[refererPath] = [];
|
|
66
|
+
}
|
|
67
|
+
errors[refererPath].push(resourcePath);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (Object.keys(errors).length === 0) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
Object.defineProperty(errors, symbols.parent, {
|
|
77
|
+
enumerable: false,
|
|
78
|
+
value: this,
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(errors, symbols.deep, {
|
|
81
|
+
enumerable: false,
|
|
82
|
+
value: true,
|
|
83
|
+
});
|
|
84
|
+
return errors;
|
|
85
|
+
}
|
|
@@ -2,13 +2,12 @@ import {
|
|
|
2
2
|
DeepObjectTree,
|
|
3
3
|
Tree,
|
|
4
4
|
deepMerge,
|
|
5
|
-
isPlainObject,
|
|
6
5
|
keysFromPath,
|
|
7
|
-
trailingSlash,
|
|
8
6
|
} from "@weborigami/async-tree";
|
|
9
7
|
import { InvokeFunctionsTransform } from "@weborigami/language";
|
|
10
8
|
import getTreeArgument from "../../common/getTreeArgument.js";
|
|
11
9
|
import crawlResources from "./crawlResources.js";
|
|
10
|
+
import { addValueToObject, getBaseUrl } from "./utilities.js";
|
|
12
11
|
|
|
13
12
|
/**
|
|
14
13
|
* Crawl a tree, starting its root index.html page, and following links to
|
|
@@ -20,6 +19,7 @@ import crawlResources from "./crawlResources.js";
|
|
|
20
19
|
*
|
|
21
20
|
* @typedef {import("@weborigami/types").AsyncTree} AsyncTree
|
|
22
21
|
* @typedef {import("@weborigami/async-tree").Treelike} Treelike
|
|
22
|
+
*
|
|
23
23
|
* @this {AsyncTree|null}
|
|
24
24
|
* @param {Treelike} treelike
|
|
25
25
|
* @param {string} [baseHref]
|
|
@@ -27,39 +27,10 @@ import crawlResources from "./crawlResources.js";
|
|
|
27
27
|
*/
|
|
28
28
|
export default async function crawlBuiltin(treelike, baseHref) {
|
|
29
29
|
const tree = await getTreeArgument(this, arguments, treelike, "site:crawl");
|
|
30
|
-
|
|
31
|
-
if (baseHref === undefined) {
|
|
32
|
-
// Ask tree or original treelike if it has an `href` property we can use as
|
|
33
|
-
// the base href to determine whether a link is local within the tree or
|
|
34
|
-
// not. If not, use a fake `local:/` base href.
|
|
35
|
-
baseHref =
|
|
36
|
-
/** @type {any} */ (tree).href ??
|
|
37
|
-
/** @type {any} */ (treelike).href ??
|
|
38
|
-
"local:/";
|
|
39
|
-
if (!baseHref?.endsWith("/")) {
|
|
40
|
-
baseHref += "/";
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
// Is the href already valid?
|
|
44
|
-
let isHrefValid = false;
|
|
45
|
-
try {
|
|
46
|
-
new URL(baseHref);
|
|
47
|
-
isHrefValid = true;
|
|
48
|
-
} catch (e) {
|
|
49
|
-
// Ignore
|
|
50
|
-
}
|
|
51
|
-
if (!isHrefValid) {
|
|
52
|
-
// Use a fake base href.
|
|
53
|
-
baseHref = `local:/${baseHref}`;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// @ts-ignore
|
|
58
|
-
const baseUrl = new URL(baseHref);
|
|
30
|
+
const baseUrl = getBaseUrl(baseHref, treelike);
|
|
59
31
|
|
|
60
32
|
const cache = {};
|
|
61
33
|
const resources = {};
|
|
62
|
-
const errors = [];
|
|
63
34
|
|
|
64
35
|
// We iterate until there are no more promises to wait for.
|
|
65
36
|
for await (const result of crawlResources(tree, baseUrl)) {
|
|
@@ -81,14 +52,6 @@ export default async function crawlBuiltin(treelike, baseHref) {
|
|
|
81
52
|
}
|
|
82
53
|
}
|
|
83
54
|
|
|
84
|
-
if (errors.length) {
|
|
85
|
-
addValueToObject(
|
|
86
|
-
cache,
|
|
87
|
-
["crawl-errors.json"],
|
|
88
|
-
JSON.stringify(errors, null, 2)
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
55
|
// Merge the cache on top of the resources tree. If we have an actual value
|
|
93
56
|
// for something already, that's better than a function that will get that
|
|
94
57
|
// value.
|
|
@@ -98,29 +61,3 @@ export default async function crawlBuiltin(treelike, baseHref) {
|
|
|
98
61
|
);
|
|
99
62
|
return result;
|
|
100
63
|
}
|
|
101
|
-
|
|
102
|
-
function addValueToObject(object, keys, value) {
|
|
103
|
-
for (let i = 0, current = object; i < keys.length; i++) {
|
|
104
|
-
const key = trailingSlash.remove(keys[i]);
|
|
105
|
-
if (i === keys.length - 1) {
|
|
106
|
-
// Write out value
|
|
107
|
-
if (isPlainObject(current[key])) {
|
|
108
|
-
// Route with existing values; treat the new value as an index.html
|
|
109
|
-
current[key]["index.html"] = value;
|
|
110
|
-
} else {
|
|
111
|
-
current[key] = value;
|
|
112
|
-
}
|
|
113
|
-
} else {
|
|
114
|
-
// Traverse further
|
|
115
|
-
if (!current[key]) {
|
|
116
|
-
current[key] = {};
|
|
117
|
-
} else if (!isPlainObject(current[key])) {
|
|
118
|
-
// Already have a value at this point. The site has a page at a route
|
|
119
|
-
// like /foo, and the site also has resources within that at routes like
|
|
120
|
-
// /foo/bar.jpg. We move the current value to "index.html".
|
|
121
|
-
current[key] = { "index.html": current[key] };
|
|
122
|
-
}
|
|
123
|
-
current = current[key];
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
@@ -129,28 +129,54 @@ async function processPath(tree, path, baseUrl) {
|
|
|
129
129
|
keys = keys.map(decodeURIComponent);
|
|
130
130
|
|
|
131
131
|
// Traverse tree to get value.
|
|
132
|
-
let value
|
|
133
|
-
|
|
134
|
-
let normalizedPath
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
132
|
+
let value;
|
|
133
|
+
let normalizedKeys;
|
|
134
|
+
let normalizedPath;
|
|
135
|
+
try {
|
|
136
|
+
value = await Tree.traverse(tree, ...keys);
|
|
137
|
+
normalizedKeys = keys.slice();
|
|
138
|
+
normalizedPath = path;
|
|
139
|
+
if (Tree.isTreelike(value)) {
|
|
140
|
+
// Path is actually a directory. See if we can get the empty string or
|
|
141
|
+
// "index.html".
|
|
142
|
+
value =
|
|
143
|
+
(await Tree.traverse(value, "")) ??
|
|
144
|
+
(await Tree.traverse(value, "index.html"));
|
|
145
|
+
if (value !== undefined) {
|
|
146
|
+
if (path.length > 0) {
|
|
147
|
+
// Mark the path as ending in a slash
|
|
148
|
+
normalizedPath = trailingSlash.add(path);
|
|
149
|
+
const key = normalizedKeys.pop();
|
|
150
|
+
normalizedKeys.push(trailingSlash.add(key));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Add index.html to keys if it's not already there
|
|
154
|
+
if (normalizedKeys.at(-1) !== "index.html") {
|
|
155
|
+
normalizedKeys.push("index.html");
|
|
156
|
+
}
|
|
147
157
|
}
|
|
158
|
+
}
|
|
148
159
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
160
|
+
if (value === undefined && path.length > 0) {
|
|
161
|
+
// The path may be a URL like `foo` or `foo/` that points to `foo.html`, so
|
|
162
|
+
// we'll try looking adding `.html` to the end. We don't want to check every
|
|
163
|
+
// path twice, so we only do this if the last key does *not* include an
|
|
164
|
+
// extension.
|
|
165
|
+
const lastKey = keys.at(-1);
|
|
166
|
+
if (lastKey !== "" && !lastKey?.includes(".")) {
|
|
167
|
+
const adjustedLastKey = `${trailingSlash.remove(lastKey)}.html`;
|
|
168
|
+
const adjustedKeys = [...keys.slice(0, -1), adjustedLastKey];
|
|
169
|
+
value = await Tree.traverse(tree, ...adjustedKeys);
|
|
170
|
+
if (value !== undefined) {
|
|
171
|
+
// Page exists at foo.html
|
|
172
|
+
normalizedPath = pathFromKeys(adjustedKeys);
|
|
173
|
+
normalizedKeys = adjustedKeys;
|
|
174
|
+
}
|
|
152
175
|
}
|
|
153
176
|
}
|
|
177
|
+
} catch (error) {
|
|
178
|
+
// Ignore errors, return empty paths below
|
|
179
|
+
value = undefined;
|
|
154
180
|
}
|
|
155
181
|
|
|
156
182
|
if (value === undefined) {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { extension, toString } from "@weborigami/async-tree";
|
|
2
|
+
import pathsInCss from "./pathsInCss.js";
|
|
3
|
+
import pathsInHtml from "./pathsInHtml.js";
|
|
4
|
+
import pathsInImageMap from "./pathsInImageMap.js";
|
|
5
|
+
import pathsInJs from "./pathsInJs.js";
|
|
6
|
+
import pathsInRobotsTxt from "./pathsInRobotsTxt.js";
|
|
7
|
+
import pathsInSitemap from "./pathsInSitemap.js";
|
|
8
|
+
|
|
9
|
+
// Filter the paths to those that are local to the site.
|
|
10
|
+
function filterPaths(paths, baseUrl, localPath) {
|
|
11
|
+
// Convert paths to absolute URLs.
|
|
12
|
+
const localUrl = new URL(localPath, baseUrl);
|
|
13
|
+
const basePathname = baseUrl.pathname;
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
const absoluteUrls = paths.map((path) => new URL(path, localUrl));
|
|
16
|
+
|
|
17
|
+
// Convert the absolute URLs to paths relative to the baseHref. If the URL
|
|
18
|
+
// points outside the tree rooted at the baseHref, the relative path will be
|
|
19
|
+
// null. We ignore the protocol in this test, because in practice sites often
|
|
20
|
+
// fumble the use of http and https, treating them interchangeably.
|
|
21
|
+
const relativePaths = absoluteUrls.map((url) => {
|
|
22
|
+
if (url.host === baseUrl.host && url.pathname.startsWith(basePathname)) {
|
|
23
|
+
const path = url.pathname.slice(basePathname.length);
|
|
24
|
+
// The process of creating the URLs will have escaped characters. We
|
|
25
|
+
// remove them. This has the side-effect of removing them if they existed
|
|
26
|
+
// in the original path; it would be better if we avoided that.
|
|
27
|
+
return decodeURIComponent(path);
|
|
28
|
+
} else {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Filter out the null paths.
|
|
34
|
+
/** @type {string[]} */
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
const filteredPaths = relativePaths.filter((path) => path);
|
|
37
|
+
return filteredPaths;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Given a value retrieved from a site using a given key (name), determine what
|
|
42
|
+
* kind of file it is and, based on that, find the paths it references.
|
|
43
|
+
*/
|
|
44
|
+
export default function findPaths(value, key, baseUrl, localPath) {
|
|
45
|
+
const text = toString(value);
|
|
46
|
+
|
|
47
|
+
// We guess the value is HTML is if its key has an .html extension or
|
|
48
|
+
// doesn't have an extension, or the value starts with `<`.
|
|
49
|
+
const ext = key ? extension.extname(key).toLowerCase() : "";
|
|
50
|
+
let foundPaths;
|
|
51
|
+
if (ext === ".html" || ext === ".htm" || ext === ".xhtml") {
|
|
52
|
+
foundPaths = pathsInHtml(text);
|
|
53
|
+
} else if (ext === ".css") {
|
|
54
|
+
foundPaths = pathsInCss(text);
|
|
55
|
+
} else if (ext === ".js") {
|
|
56
|
+
foundPaths = pathsInJs(text);
|
|
57
|
+
} else if (ext === ".map") {
|
|
58
|
+
foundPaths = pathsInImageMap(text);
|
|
59
|
+
} else if (key === "robots.txt") {
|
|
60
|
+
foundPaths = pathsInRobotsTxt(text);
|
|
61
|
+
} else if (key === "sitemap.xml") {
|
|
62
|
+
foundPaths = pathsInSitemap(text);
|
|
63
|
+
} else if (ext === "" && text?.trim().startsWith("<")) {
|
|
64
|
+
// Probably HTML
|
|
65
|
+
foundPaths = pathsInHtml(text);
|
|
66
|
+
} else {
|
|
67
|
+
// Doesn't have an extension we want to process
|
|
68
|
+
return {
|
|
69
|
+
crawlablePaths: [],
|
|
70
|
+
resourcePaths: [],
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const crawlablePaths = filterPaths(
|
|
75
|
+
foundPaths.crawlablePaths,
|
|
76
|
+
baseUrl,
|
|
77
|
+
localPath
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const resourcePaths = filterPaths(
|
|
81
|
+
foundPaths.resourcePaths,
|
|
82
|
+
baseUrl,
|
|
83
|
+
localPath
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
crawlablePaths,
|
|
88
|
+
resourcePaths,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { parse, walk } from "css-tree";
|
|
2
|
+
import { addHref } from "./utilities.js";
|
|
3
|
+
|
|
4
|
+
const imageFunctions = ["cross-fade", "image", "image-set"];
|
|
5
|
+
|
|
6
|
+
export default function pathsInCss(css, context = "stylesheet") {
|
|
7
|
+
const paths = {
|
|
8
|
+
crawlablePaths: [],
|
|
9
|
+
resourcePaths: [],
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
let ast;
|
|
13
|
+
try {
|
|
14
|
+
ast = parse(css, { context });
|
|
15
|
+
} catch (e) {
|
|
16
|
+
// If the CSS is invalid, we can't parse it, so we can't extract paths. For
|
|
17
|
+
// now we just return no paths.
|
|
18
|
+
return paths;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!ast) {
|
|
22
|
+
// Unclear why parser sometimes returns an undefined AST
|
|
23
|
+
return paths;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
walk(
|
|
27
|
+
ast,
|
|
28
|
+
/** @this {any} */
|
|
29
|
+
function (node) {
|
|
30
|
+
const { type, value } = node;
|
|
31
|
+
if (
|
|
32
|
+
this.atrule?.name === "import" &&
|
|
33
|
+
(type === "String" || type === "Url")
|
|
34
|
+
) {
|
|
35
|
+
// A plain string or url() in an @import
|
|
36
|
+
addHref(paths, value, true);
|
|
37
|
+
} else if (
|
|
38
|
+
type === "String" &&
|
|
39
|
+
imageFunctions.includes(this.function?.name)
|
|
40
|
+
) {
|
|
41
|
+
// A plain string in an cross-fade(), image(), or image-set()
|
|
42
|
+
addHref(paths, value, false);
|
|
43
|
+
} else if (type === "Url") {
|
|
44
|
+
// A url() anywhere else
|
|
45
|
+
addHref(paths, value, false);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
return paths;
|
|
51
|
+
}
|