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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. 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.0",
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",
@@ -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
- // The marker is a CLOSING tag, so its own indentation is the parent's. Children sit one
77
- // level in, and every line of a multi-line snippet gets the same treatment or the
78
- // continuation lines land wherever they happened to be authored.
79
- const indent = source.slice(lineStart, at).match(/^\s*/)[0] + " ";
80
- // Keep the snippet's OWN relative indentation: a multi-line tag authors its attributes
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
- return source.slice(0, lineStart) + body + "\n" + source.slice(lineStart);
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. */