human-machine-swapper 1.2.0 → 1.2.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/package.json +1 -1
- package/src/init/apply.mjs +17 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "human-machine-swapper",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "A web component that adds a Human / Machine pill to any website. Machine shows the site's llms.txt, the plain-text reading written for language models, rendered in place. One file, about 6 KB gzipped, no dependencies and no build step.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"llms.txt",
|
package/src/init/apply.mjs
CHANGED
|
@@ -73,18 +73,27 @@ function insertBefore(source, marker, snippet) {
|
|
|
73
73
|
const at = source.indexOf(marker);
|
|
74
74
|
if (at === -1) return null;
|
|
75
75
|
const lineStart = source.lastIndexOf("\n", at) + 1;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
//
|
|
81
|
-
// one level in and its closing bracket flush, and flattening that puts `></script>`
|
|
76
|
+
const before = source.slice(lineStart, at);
|
|
77
|
+
const indent = before.match(/^\s*/)[0];
|
|
78
|
+
|
|
79
|
+
// Indent every line of the snippet one level in from the closing tag, keeping the
|
|
80
|
+
// snippet's own relative indentation so a multi-line tag's `></script>` does not end up
|
|
82
81
|
// deeper than the tag it closes.
|
|
83
82
|
const body = snippet
|
|
84
83
|
.split("\n")
|
|
85
|
-
.map((l) => (l.trim() ? indent + l : l))
|
|
84
|
+
.map((l) => (l.trim() ? indent + " " + l : l))
|
|
86
85
|
.join("\n");
|
|
87
|
-
|
|
86
|
+
|
|
87
|
+
// Two shapes, and getting this wrong writes broken HTML into somebody's repo.
|
|
88
|
+
//
|
|
89
|
+
// When the closing tag starts its own line, insert a line above it. When it does NOT -
|
|
90
|
+
// `<head><meta /><title>x</title></head>` all on one line - inserting "before the line"
|
|
91
|
+
// puts the tags OUTSIDE the element entirely, above <head>. That is what happened to the
|
|
92
|
+
// first Astro site this ran on. Split the line instead and keep the content inside.
|
|
93
|
+
if (before.trim() === "") {
|
|
94
|
+
return source.slice(0, lineStart) + body + "\n" + source.slice(lineStart);
|
|
95
|
+
}
|
|
96
|
+
return source.slice(0, at) + "\n" + body + "\n" + indent + source.slice(at);
|
|
88
97
|
}
|
|
89
98
|
|
|
90
99
|
/** Already installed? Then there is nothing to do and we say so instead of doubling up. */
|