Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** @jsxImportSource https://esm.sh/react */
import React, { useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
function App() {
const [input, setInput] = useState({
storyTitle: "",
storyText: "",
});
const [result, setResult] = useState(null);
const [loading, setLoading] = useState(false);
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);
const response = await fetch("/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(input),
});
const data = await response.json();
setResult(data);
setLoading(false);
};
return (
<div>
<h1>Story Processor for Algolia</h1>
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="storyTitle">Story Title:</label>
<input
type="text"
id="storyTitle"
value={input.storyTitle}
onChange={(e) => setInput({ ...input, storyTitle: e.target.value })}
required
/>
</div>
<div>
<label htmlFor="storyText">Story Text:</label>
<textarea
id="storyText"
value={input.storyText}
onChange={(e) => setInput({ ...input, storyText: e.target.value })}
required
rows="20"
>
</textarea>
</div>
<button type="submit" disabled={loading}>
{loading ? "Processing..." : "Process story"}
</button>
</form>
{result && (
<div>
<h2>Results:</h2>
<pre>{JSON.stringify(result, null, 2)}</pre>
</div>
)}
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") { client(); }
function preparestoryForAlgolia(storyTitle, storyText) {
const generateUniqueID = (() => {
let counter = 1000000000; // Start from 1 billion
return (storyTitle) => {
const sanitizedTitle = storyTitle.replace(/[^a-z0-9]/gi, "_").toLowerCase();
return `${sanitizedTitle}_${counter--}`; // Count down
};
})();
function isWordBoundary(text, index) {
if (index === 0 || index === text.length) return true;
const prevChar = text[index - 1];
const currChar = text[index];
if (currChar === " ") return true;
if (prevChar === "-" && /[a-zA-Z]/.test(currChar)) return false;
if (currChar === "-" && /[a-zA-Z]/.test(prevChar)) return false;
if (prevChar === "'" && /[a-zA-Z]/.test(currChar)) return false;
if (/\w/.test(prevChar) && !/\w/.test(currChar)) return true;
return false;
}
function createAlgoliaRecord(storyContent, startIndex, endIndex, storyTitle, chapterTitle, chunkIndex) {
return {
storyTitle: storyTitle,
objectID: generateUniqueID(storyTitle),
chapterTitle: chapterTitle,
storyContent: storyContent,
startIndex: startIndex,
endIndex: endIndex,
chunkIndex: chunkIndex,
_tags: [`story:${storyTitle}`, `chapter:${chapterTitle}`],
willthereader-recordwebsitepreparer.web.val.run
September 14, 2024