slop-editor 0.0.0-stage → 0.1.0
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 +21 -0
- package/README.md +133 -2
- package/cli.mjs +235 -0
- package/package.json +39 -4
- package/slop.css +209 -0
- package/slop.js +1179 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Raphael Bajet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,134 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Slop editor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
It tracks who wrote every word on a page: the AI, you, or nobody (facts and quotes). Readers get a **Slop-o-meter** switch that colors the words by author. You get an editor where you read what your agent wrote, fix it, and sign off on what you agree with.
|
|
4
|
+
|
|
5
|
+
Every text starts as AI-generated: slop. What you type becomes yours. Switch a text's **slop** off and the AI's words are approved as they are, in a color of their own. Switch it back on to take that back. The point isn't detection. It's owning what you publish.
|
|
6
|
+
|
|
7
|
+
It's two files (`slop.js` and `slop.css`), has no dependencies and no build step, and works in any HTML page.
|
|
8
|
+
|
|
9
|
+
**[Try it on the demo page](https://baphaelman.github.io/slop-editor/)**, which is itself tracked by the slop editor.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
**With Claude Code**, add the plugin, then ask for the slop editor on a page:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
/plugin marketplace add baphaelman/slop-editor
|
|
17
|
+
/plugin install slop-editor@slop-editor
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The agent moves the page's words into the text block for you. That's the tedious part.
|
|
21
|
+
|
|
22
|
+
**Without it**, use the command line (Node 18+):
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
npx slop-editor install page.html # inline slop.css and slop.js, add the text block
|
|
26
|
+
npx slop-editor edit page.html # open the page with the editor; it saves into the file
|
|
27
|
+
npx slop-editor check page.html # how much is yours, repeated text, texts the block lacks
|
|
28
|
+
npx slop-editor sync page.html # re-inline the current slop.css and slop.js
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Or link the files instead of inlining them: `<link rel="stylesheet" href="slop.css">` and `<script src="slop.js"></script>`.
|
|
32
|
+
|
|
33
|
+
## The text block
|
|
34
|
+
|
|
35
|
+
The words live in the page, in `<script type="application/json" id="slop-text">`:
|
|
36
|
+
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"version": 3,
|
|
40
|
+
"intro": {
|
|
41
|
+
"title": "How it works",
|
|
42
|
+
"p1": ["The agent wrote this. ", { "me": "I wrote this. " }, "Then ", { "data": "\"a quote\"" }, "."]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
A **text** is a plain string (AI) or a list mixing strings (AI), `{"me": "..."}` (yours), `{"ok": "..."}` (the AI's, approved), `{"data": "..."}` (facts, or other people's words) and `{"<category>": "..."}` for a category of the page's own. Groups are objects, in reading order. Top-level keys starting with `_` aren't text.
|
|
48
|
+
|
|
49
|
+
**Categories.** Co-authors, editors and sources can have categories of their own, with a label and a color, in `_categories`. The same object holds colors picked for the built-in ones:
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
"_categories": {
|
|
53
|
+
"bob": { "label": "Bob", "color": "#2b9fc0" },
|
|
54
|
+
"ai": { "color": "#d01f4a" }
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Slop is only ever the AI's words. Other categories are other people's. Inline markup: `[label](url)`, `**bold**`, `*italic*`, `` `code` ``, `[[key]]` for a key cap, `{name}` for a value the page fills in, and `\` to escape.
|
|
59
|
+
|
|
60
|
+
In the page:
|
|
61
|
+
|
|
62
|
+
```html
|
|
63
|
+
<div data-slop-meter></div> <!-- what readers see, if anything -->
|
|
64
|
+
<h2 data-slop="intro.title"></h2> <!-- filled from the block -->
|
|
65
|
+
<p data-slop="intro.p1"></p>
|
|
66
|
+
<script>
|
|
67
|
+
Slop.vars.count = 42; // {count} in a text: a fact, shown as data
|
|
68
|
+
Slop.onChange(() => relayout()); // after an edit: redo anything built from the text
|
|
69
|
+
Slop.start();
|
|
70
|
+
</script>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Code can render texts itself with `Slop.html('intro.p1')` (HTML) or `Slop.words('intro.p1')` (plain text).
|
|
74
|
+
|
|
75
|
+
A text the page shows only some of the time (after a click, in another tab, once something's been pasted) is tagged **not on the page** in the editor. To let the editor show it while it's being edited, tell it how:
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
Slop.reveal('intro.note', () => {
|
|
79
|
+
showNote = true; render(); // put the page in a state that shows it
|
|
80
|
+
return () => { showNote = false; render(); }; // and undo that afterwards
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
A group's key (`'intro'`) covers all its texts. While shown, the page's own redraws must keep it shown (here, `render()` checks `showNote`).
|
|
85
|
+
|
|
86
|
+
## The editor
|
|
87
|
+
|
|
88
|
+
`slop-editor edit page.html` serves the page's folder on 127.0.0.1 and opens the page with an **Edit text** button. Published, the page shows no sign of the editor. `Slop.start({edit: 'local'})` gives every visitor an editor that saves in their browser instead (the demo page uses it).
|
|
89
|
+
|
|
90
|
+
- **All** shows every text, grouped by section. **Slop left** shows only the texts that aren't all yours yet; work through it until it's empty. **Repeats** shows the repeated text.
|
|
91
|
+
- Each text, section and the whole page has a pie of what it's made of and a **slop** switch. Off approves the AI's words; on takes that back. Your typed words are never touched. Every switch can be undone from the status line.
|
|
92
|
+
- At the top: how much of the page is slop, and every category with its share and a color picker. Rename a category by editing its name. **+ Category** adds one; × deletes it (its words become AI-generated).
|
|
93
|
+
- Focus a text and a row of categories appears under it: **Mark all as** (or **Mark selection as**, with words selected) gives the text, or just the selection, to a category.
|
|
94
|
+
- Click any text on the page to jump to it in the editor. Focus a text in the editor to find it on the page.
|
|
95
|
+
- Drag the header to move the editor anywhere, and drag any edge or corner to resize it. It stays where you leave it; double-click the header to put it back. On a phone it's a sheet along the bottom, and dragging changes its height.
|
|
96
|
+
- Nothing is written until you click **Save** (or press ⌘S). The button counts the texts changed since the last save. Each changed text has a **Revert** link that takes its words and authorship back to the last save. Saves are versioned: a tab whose copy of the file is out of date gets an error instead of overwriting newer edits, and leaving with unsaved changes asks first.
|
|
97
|
+
- ⌘Z in a text undoes typing and gives the restored words back to whoever wrote them. ⌘Z anywhere else undoes the last switch, mark or revert.
|
|
98
|
+
|
|
99
|
+
### Repeated text
|
|
100
|
+
|
|
101
|
+
Agents repeat things: a heading that's also in the nav, a tagline in the hero and the footer. When several texts have the same words, the editor treats them as copies of one text:
|
|
102
|
+
|
|
103
|
+
- Each copy keeps its own authorship. A copy is slop until it's approved, even if it repeats your words.
|
|
104
|
+
- Each copy has a **2×** badge. In **All**, a copy's switch changes that copy only, and says so: "Only this copy changed. 1 other copy didn't. Change it too".
|
|
105
|
+
- The **Repeats** tab has one card per repeated text. Its switch and its text change every copy, and it lists where each copy is.
|
|
106
|
+
- Edit one copy on its own and the editor offers to change the others too.
|
|
107
|
+
|
|
108
|
+
## Colors
|
|
109
|
+
|
|
110
|
+
Slop is purple, approved AI text is green, data is amber, and your words keep the page's own color, until you pick others in the editor (they're saved in `_categories`). The defaults follow the page's background (light or dark). A page can also set them in CSS with `--slop-ai`, `--slop-ok`, `--slop-data` and `--slop-me`, and the editor's fonts with `--slop-font` and `--slop-mono`.
|
|
111
|
+
|
|
112
|
+
## What readers see
|
|
113
|
+
|
|
114
|
+
It's the page's choice, made in the editor (**Readers see**) and saved in the block as `"_show"`:
|
|
115
|
+
|
|
116
|
+
- **Nothing** (the default): no sign of the slop editor. The safe choice for a page or project you don't know.
|
|
117
|
+
- **Pie chart**: a pie of who wrote what, and a legend: each category in its color, with its share of the page's text.
|
|
118
|
+
- **Colors**: the Slop-o-meter, the pie and legend plus a switch that colors every word by who wrote it.
|
|
119
|
+
|
|
120
|
+
It shows where the page puts `<div data-slop-meter></div>`. While the editor is open, you see the colors whatever readers get.
|
|
121
|
+
|
|
122
|
+
## Rules for agents
|
|
123
|
+
|
|
124
|
+
The full rules are in [skills/slop-editor/SKILL.md](skills/slop-editor/SKILL.md), which the Claude Code plugin loads. In short:
|
|
125
|
+
|
|
126
|
+
- Write everything as AI text to start. Only what the user dictates word for word goes in as `{"me": ...}`.
|
|
127
|
+
- Other people's words go in their category (`{"data": ...}` for quotes, or one of the page's own).
|
|
128
|
+
- Never reword the user's words without asking.
|
|
129
|
+
- Don't approve anything (`ok`) or take approval back unless asked.
|
|
130
|
+
- When editing the block by hand while the editor is open: read the file first, bump `version`, and keep every `"me"` and `"ok"` piece word for word.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT
|
package/cli.mjs
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The slop editor's command line. No dependencies.
|
|
3
|
+
//
|
|
4
|
+
// slop-editor install <page.html> add the text block and inline slop.css and slop.js into a page (it stays one file)
|
|
5
|
+
// slop-editor sync <page.html …> re-inline the current slop.css and slop.js into pages that have them
|
|
6
|
+
// slop-editor edit <page.html> serve the page's folder on localhost with the editor, which saves into the file
|
|
7
|
+
// [--port 5178] [--no-open]
|
|
8
|
+
// slop-editor check <page.html> what the text is made of, the repeated text, and texts the page uses but the block lacks
|
|
9
|
+
import http from 'node:http';
|
|
10
|
+
import net from 'node:net';
|
|
11
|
+
import { readFile, writeFile, rename, stat } from 'node:fs/promises';
|
|
12
|
+
import { spawn } from 'node:child_process';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { fileURLToPath } from 'node:url';
|
|
15
|
+
|
|
16
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
17
|
+
const STYLE = /(<style id="slop-css">)[\s\S]*?(<\/style>)/;
|
|
18
|
+
const SCRIPT = /(<script id="slop-js">)[\s\S]*?(<\/script>)/;
|
|
19
|
+
const BLOCK = /(<script type="application\/json" id="slop-text">)([\s\S]*?)(<\/script>)/;
|
|
20
|
+
|
|
21
|
+
// the format's functions, from slop.js itself (it returns only those outside a browser)
|
|
22
|
+
const Slop = new Function(`${await readFile(path.join(here, 'slop.js'), 'utf8')}\nreturn Slop;`)();
|
|
23
|
+
|
|
24
|
+
function readBlock(html, name) {
|
|
25
|
+
const m = BLOCK.exec(html);
|
|
26
|
+
if (!m) throw new Error(`${name} has no <script type="application/json" id="slop-text"> block (run install first)`);
|
|
27
|
+
return JSON.parse(m[2]);
|
|
28
|
+
}
|
|
29
|
+
// JSON inside a <script>: "</" would end the element early
|
|
30
|
+
const serialize = (text) => '\n' + JSON.stringify(text, null, 2).replace(/<\//g, '<\\/') + '\n';
|
|
31
|
+
const inline = (s) => s.trimEnd().replace(/<\/(script|style)/gi, '<\\/$1');
|
|
32
|
+
|
|
33
|
+
/* ---------- install and sync ---------- */
|
|
34
|
+
|
|
35
|
+
async function sync(file) {
|
|
36
|
+
const css = inline(await readFile(path.join(here, 'slop.css'), 'utf8'));
|
|
37
|
+
const js = inline(await readFile(path.join(here, 'slop.js'), 'utf8'));
|
|
38
|
+
const html = await readFile(file, 'utf8');
|
|
39
|
+
if (!STYLE.test(html) || !SCRIPT.test(html)) throw new Error(`${file}: no slop-css or slop-js markers (run install first)`);
|
|
40
|
+
const next = html.replace(STYLE, (_, a, b) => `${a}\n${css}\n${b}`).replace(SCRIPT, (_, a, b) => `${a}\n${js}\n${b}`);
|
|
41
|
+
if (next !== html) await writeFile(file, next);
|
|
42
|
+
console.log(`${next === html ? 'up to date' : 'synced'}: ${path.relative(process.cwd(), file)}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// the styles go at the end of <head>; the block and the code just before the page's first inline script (so that
|
|
46
|
+
// script can use Slop), or at the end of <body> with a Slop.start() of their own
|
|
47
|
+
function install(html) {
|
|
48
|
+
let out = html;
|
|
49
|
+
if (!STYLE.test(out)) {
|
|
50
|
+
const tag = '<style id="slop-css"></style>\n';
|
|
51
|
+
out = /<\/head>/i.test(out) ? out.replace(/<\/head>/i, `${tag}</head>`) : tag + out;
|
|
52
|
+
}
|
|
53
|
+
if (BLOCK.test(out) && SCRIPT.test(out)) return out;
|
|
54
|
+
const parts = [];
|
|
55
|
+
if (!BLOCK.test(out)) parts.push('<script type="application/json" id="slop-text">\n{\n "version": 1\n}\n</script>');
|
|
56
|
+
if (!SCRIPT.test(out)) parts.push('<script id="slop-js"></script>');
|
|
57
|
+
const tag = parts.join('\n') + '\n';
|
|
58
|
+
const first = /<script(?![^>]*\bsrc=)(?![^>]*\bid="slop-)(?![^>]*type="application\/(?:ld\+)?json")[^>]*>/i.exec(out);
|
|
59
|
+
if (first) return out.slice(0, first.index) + tag + out.slice(first.index);
|
|
60
|
+
if (/<\/body>/i.test(out)) return out.replace(/<\/body>/i, `${tag}<script>Slop.start();</script>\n</body>`);
|
|
61
|
+
return `${out}\n${tag}<script>Slop.start();</script>\n`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/* ---------- check ---------- */
|
|
65
|
+
|
|
66
|
+
async function check(file) {
|
|
67
|
+
const html = await readFile(file, 'utf8');
|
|
68
|
+
const text = readBlock(html, file);
|
|
69
|
+
const entries = [];
|
|
70
|
+
const walk = (node, at) => Object.entries(node).forEach(([k, v]) => {
|
|
71
|
+
if (!at.length && (k === 'version' || k.startsWith('_'))) return; // not text
|
|
72
|
+
if (typeof v === 'string' || Array.isArray(v)) entries.push([[...at, k].join('.'), v]);
|
|
73
|
+
else if (v && typeof v === 'object') walk(v, [...at, k]);
|
|
74
|
+
});
|
|
75
|
+
walk(text, []);
|
|
76
|
+
const paths = new Set(entries.map(([p]) => p));
|
|
77
|
+
const m = Slop.makeup(entries.map(([, v]) => v));
|
|
78
|
+
const label = (id) => text._categories?.[id]?.label || Slop.BUILTIN[id]?.label || id;
|
|
79
|
+
console.log(`${path.relative(process.cwd(), file)} (version ${text.version})`);
|
|
80
|
+
console.log(` ${m.allWords.toLocaleString()} words in ${entries.length} texts`);
|
|
81
|
+
console.log(` ${Object.entries(m.counts).sort((a, b) => b[1] - a[1]).map(([id, n]) => `${label(id)} ${Slop.percent(n / m.letters)}`).join(' · ')}`);
|
|
82
|
+
const left = entries.filter(([, v]) => Slop.makeup([v]).counts.ai);
|
|
83
|
+
console.log(` ${left.length} ${left.length === 1 ? 'text has' : 'texts have'} slop left`);
|
|
84
|
+
const sets = Slop.repeats(entries);
|
|
85
|
+
if (sets.length) console.log(` repeated text:\n${sets.map((r) => ` "${r.words.slice(0, 60)}${r.words.length > 60 ? '…' : ''}": ${r.paths.join(', ')}`).join('\n')}`);
|
|
86
|
+
// texts the page asks for: data-slop="…" in a tag, Slop.html('…') and Slop.words('…') in its code. Only a whole
|
|
87
|
+
// string counts: Slop.html('caption' + n) builds its path, so its start isn't a text
|
|
88
|
+
const page = html.replace(SCRIPT, '').replace(STYLE, '').replace(BLOCK, ''); // the page's own markup and code
|
|
89
|
+
const used = new Set([...page.matchAll(/<[a-z][^>]*?\bdata-slop="([^"]+)"/gi), ...page.matchAll(/Slop\.(?:html|words|get)\(\s*(['"`])([\w.-]+)\1\s*[,)]/g)].map((m) => m[2] ?? m[1]));
|
|
90
|
+
const missing = [...used].filter((p) => !paths.has(p));
|
|
91
|
+
if (missing.length) console.log(` missing from the block: ${missing.join(', ')}`);
|
|
92
|
+
return missing.length ? 1 : 0;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/* ---------- edit ---------- */
|
|
96
|
+
|
|
97
|
+
const TYPES = {
|
|
98
|
+
'.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.mjs': 'text/javascript; charset=utf-8',
|
|
99
|
+
'.css': 'text/css; charset=utf-8', '.json': 'application/json', '.svg': 'image/svg+xml', '.png': 'image/png',
|
|
100
|
+
'.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp', '.ico': 'image/x-icon',
|
|
101
|
+
'.woff2': 'font/woff2', '.mp3': 'audio/mpeg', '.wav': 'audio/wav', '.mp4': 'video/mp4', '.txt': 'text/plain; charset=utf-8',
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// POST /__slop takes the whole text block. Its `version` must be the file's current one (otherwise 409: the file
|
|
105
|
+
// changed since the page loaded it), and every save bumps it. A GET answers "slop-editor": that's how the page knows
|
|
106
|
+
async function edit(file, { port, fixed, open }) {
|
|
107
|
+
const root = path.dirname(file);
|
|
108
|
+
const name = path.basename(file);
|
|
109
|
+
readBlock(await readFile(file, 'utf8'), name); // fail now, not on the first save
|
|
110
|
+
let queue = Promise.resolve(); // one save at a time
|
|
111
|
+
const save = (body) => {
|
|
112
|
+
const run = queue.then(async () => {
|
|
113
|
+
const text = JSON.parse(body);
|
|
114
|
+
if (!text || typeof text !== 'object' || Array.isArray(text) || typeof text.version !== 'number') {
|
|
115
|
+
return [400, 'expected an object with a numeric "version"'];
|
|
116
|
+
}
|
|
117
|
+
const html = await readFile(file, 'utf8');
|
|
118
|
+
const current = readBlock(html, name);
|
|
119
|
+
if (text.version !== current.version) return [409, 'the file changed since this page loaded it. Reload the page (copy any unsaved edits first)'];
|
|
120
|
+
const saved = { ...text, version: current.version + 1 };
|
|
121
|
+
const tmp = `${file}.saving`;
|
|
122
|
+
await writeFile(tmp, html.replace(BLOCK, (_, a, __, b) => a + serialize(saved) + b));
|
|
123
|
+
await rename(tmp, file);
|
|
124
|
+
return [200, String(saved.version)];
|
|
125
|
+
});
|
|
126
|
+
queue = run.catch(() => {});
|
|
127
|
+
return run;
|
|
128
|
+
};
|
|
129
|
+
const server = http.createServer(async (req, res) => {
|
|
130
|
+
const url = new URL(req.url, 'http://localhost');
|
|
131
|
+
const send = (status, body, type = 'text/plain; charset=utf-8') => {
|
|
132
|
+
res.writeHead(status, { 'content-type': type, 'cache-control': 'no-store' });
|
|
133
|
+
res.end(body);
|
|
134
|
+
};
|
|
135
|
+
if (url.pathname === '/__slop') {
|
|
136
|
+
if (req.method !== 'POST') return send(200, 'slop-editor');
|
|
137
|
+
let body = '';
|
|
138
|
+
req.on('data', (chunk) => { body += chunk; });
|
|
139
|
+
req.on('end', async () => {
|
|
140
|
+
try {
|
|
141
|
+
const [status, out] = await save(body);
|
|
142
|
+
if (status === 200) console.log(`saved version ${out}`);
|
|
143
|
+
send(status, out);
|
|
144
|
+
} catch (e) {
|
|
145
|
+
send(400, e.message);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') return send(405, '');
|
|
151
|
+
const target = path.resolve(root, '.' + decodeURIComponent(url.pathname === '/' ? `/${name}` : url.pathname));
|
|
152
|
+
if (!target.startsWith(root + path.sep)) return send(403, 'outside the page\'s folder');
|
|
153
|
+
try {
|
|
154
|
+
if (!(await stat(target)).isFile()) return send(404, 'not found');
|
|
155
|
+
send(200, await readFile(target), TYPES[path.extname(target).toLowerCase()] ?? 'application/octet-stream');
|
|
156
|
+
} catch {
|
|
157
|
+
send(404, 'not found');
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
// a port that's taken: the next one, unless the port was asked for. Taken means anything answers there, on
|
|
161
|
+
// 127.0.0.1 or ::1: `localhost` can be either, and another dev server (Vite, say) may listen on ::1 only, where
|
|
162
|
+
// binding 127.0.0.1 wouldn't notice it
|
|
163
|
+
const answers = (host) => new Promise((resolve) => {
|
|
164
|
+
const socket = net.connect({ port, host });
|
|
165
|
+
const done = (yes) => { socket.destroy(); resolve(yes); };
|
|
166
|
+
socket.once('connect', () => done(true)).once('error', () => done(false)).setTimeout(500, () => done(false));
|
|
167
|
+
});
|
|
168
|
+
const first = port;
|
|
169
|
+
const taken = async () => (await answers('127.0.0.1')) || (await answers('::1'));
|
|
170
|
+
const next = async () => {
|
|
171
|
+
while (await taken()) {
|
|
172
|
+
if (fixed || port >= first + 20) {
|
|
173
|
+
console.error(`port ${port} is taken: try --port ${port + 1}`);
|
|
174
|
+
process.exit(1);
|
|
175
|
+
}
|
|
176
|
+
port++;
|
|
177
|
+
}
|
|
178
|
+
server.listen(port, '127.0.0.1');
|
|
179
|
+
};
|
|
180
|
+
server.on('error', (e) => {
|
|
181
|
+
if (e.code === 'EADDRINUSE' && !fixed && port < first + 20) { port++; next(); return; }
|
|
182
|
+
console.error(e.code === 'EADDRINUSE' ? `port ${port} is taken: try --port ${port + 1}` : e.message);
|
|
183
|
+
process.exit(1);
|
|
184
|
+
});
|
|
185
|
+
server.on('listening', () => {
|
|
186
|
+
if (port !== first) console.log(`(port ${first} is taken)`);
|
|
187
|
+
const link = `http://127.0.0.1:${port}/${encodeURIComponent(name)}`;
|
|
188
|
+
console.log(`Editing ${name}: ${link}\nThe editor saves into the file. Ctrl+C to stop.`);
|
|
189
|
+
if (!open) return;
|
|
190
|
+
const [cmd, ...rest] = process.platform === 'darwin' ? ['open'] : process.platform === 'win32' ? ['cmd', '/c', 'start', ''] : ['xdg-open'];
|
|
191
|
+
spawn(cmd, [...rest, link], { stdio: 'ignore', detached: true }).on('error', () => {}).unref();
|
|
192
|
+
});
|
|
193
|
+
await next();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/* ---------- main ---------- */
|
|
197
|
+
|
|
198
|
+
const args = process.argv.slice(2);
|
|
199
|
+
const flag = (name) => { const i = args.indexOf(name); return i >= 0 ? args.splice(i, 2)[1] : null; };
|
|
200
|
+
const portFlag = flag('--port') ?? process.env.PORT;
|
|
201
|
+
const port = Number(portFlag ?? 5178);
|
|
202
|
+
const noOpen = args.includes('--no-open') && args.splice(args.indexOf('--no-open'), 1);
|
|
203
|
+
const [cmd, ...rest] = args;
|
|
204
|
+
const files = rest.map((f) => path.resolve(f));
|
|
205
|
+
const usage = 'usage: slop-editor install <page.html> | sync <page.html …> | edit <page.html> [--port N] [--no-open] | check <page.html …>';
|
|
206
|
+
|
|
207
|
+
if (cmd !== 'help' && cmd && !files.length) {
|
|
208
|
+
console.error(usage);
|
|
209
|
+
process.exit(1);
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
if (cmd === 'install') {
|
|
213
|
+
for (const file of files) {
|
|
214
|
+
await writeFile(file, install(await readFile(file, 'utf8')));
|
|
215
|
+
await sync(file);
|
|
216
|
+
}
|
|
217
|
+
console.log('\nNext: mark text elements with data-slop="group.key" and put their words in the slop-text block\n'
|
|
218
|
+
+ '(as AI text to start). Add <div data-slop-meter></div> at the top (readers see\n'
|
|
219
|
+
+ 'nothing there unless you pick a pie chart or colors in the editor). Call Slop.start() once Slop.vars is\n'
|
|
220
|
+
+ 'set and before anything reads the text. Then: slop-editor edit <page.html>');
|
|
221
|
+
} else if (cmd === 'sync') {
|
|
222
|
+
for (const file of files) await sync(file);
|
|
223
|
+
} else if (cmd === 'edit') {
|
|
224
|
+
await edit(files[0], { port, fixed: portFlag != null, open: !noOpen });
|
|
225
|
+
} else if (cmd === 'check') {
|
|
226
|
+
let status = 0;
|
|
227
|
+
for (const file of files) status |= await check(file);
|
|
228
|
+
process.exitCode = status;
|
|
229
|
+
} else {
|
|
230
|
+
console.log(usage);
|
|
231
|
+
}
|
|
232
|
+
} catch (e) {
|
|
233
|
+
console.error(e.message);
|
|
234
|
+
process.exit(1);
|
|
235
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "slop-editor",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A slop editor: track who wrote every word on a page (the AI, you, or nobody), then read the slop and make it yours.",
|
|
5
|
+
"homepage": "https://baphaelman.github.io/slop-editor/",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/baphaelman/slop-editor.git"
|
|
9
|
+
},
|
|
10
|
+
"bugs": "https://github.com/baphaelman/slop-editor/issues",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"bin": {
|
|
13
|
+
"slop-editor": "cli.mjs"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"cli.mjs",
|
|
17
|
+
"slop.js",
|
|
18
|
+
"slop.css",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "node --test test.mjs",
|
|
24
|
+
"test:browser": "node browser-test.mjs"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"ai",
|
|
31
|
+
"slop",
|
|
32
|
+
"writing",
|
|
33
|
+
"authorship",
|
|
34
|
+
"editor"
|
|
35
|
+
],
|
|
36
|
+
"author": "Raphael Bajet",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"playwright-core": "^1.65.1"
|
|
40
|
+
}
|
|
41
|
+
}
|