Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/site/scripts/reader-body-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ export const compileReaderNodes = (
return Schema.decodeSync(Schema.Array(ReaderNodeSchema))(converted);
};

export const compileReaderSnippet = (html: string, sourcePath: string) =>
compileReaderNodes(parseDocument(html).children, sourcePath);

const readBody = (
html: string,
sourcePath: string,
Expand Down
1 change: 1 addition & 0 deletions apps/web/effect-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Plugin } from "vite";
const entries = {
"directory-effect-ui": "src/client/directory-effect-ui/entry.ts",
"news-effect-ui": "src/client/news-effect-ui/entry.ts",
"reader-effect-ui": "src/client/reader-effect-ui/entry.ts",
};

const manifestFile = ".vite/manifest.json";
Expand Down
218 changes: 218 additions & 0 deletions apps/web/scripts/reader-effect-ui-legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
import { Effect, Option } from "effect";

import { compileReaderSnippet } from "../../site/scripts/reader-body-document.js";
import { ReaderInputError } from "../../site/scripts/reader-input-error.js";
import { ReaderBlock, ReaderInline } from "../src/client/reader-document.js";
import type { ReaderInlineValue } from "../src/client/reader-document.js";
import { ReaderNode } from "../src/client/reader-node.js";
import type { ReaderNodeValue } from "../src/client/reader-node.js";
import type { ReaderPageFlags } from "../src/client/reader/model.js";
import { readerParagraphAnchor } from "../src/features/reader-paragraph.js";

type Element = ReturnType<typeof ReaderNode.Element>;

const headingTags = { 2: "h2", 3: "h3", 4: "h4" } as const;

const text = (value: string) => ReaderNode.Text({ value });

const element = (
tag: Element["tag"],
children: readonly ReaderNodeValue[],
attributes: Element["attributes"] = []
) => ReaderNode.Element({ attributes, children, tag });

const attributed = (
tag: Element["tag"],
name: Element["attributes"][number]["name"],
value: string,
children: readonly ReaderNodeValue[]
) => element(tag, children, [{ name, value }]);

const link = (href: string, label: string) =>
attributed("a", "href", href, [text(label)]);

const renderInline = (
inline: ReaderInlineValue,
counts: Readonly<Record<string, number>>,
pagePath: string
): ReaderNodeValue =>
ReaderInline.$match(inline, {
Code: ({ value }) => element("code", [text(value)]),
Emphasis: ({ content }) =>
element(
"em",
content.map((child) => renderInline(child, counts, pagePath))
),
Link: ({ href, value }) => {
const anchor = link(href, value);
const count = counts[href] ?? 0;

if (
href === pagePath ||
!/^\/(?:lore|systems|skills)\//u.test(href) ||
count < 2
) {
return anchor;
}

return element("span", [
anchor,
attributed("sup", "class", "inbound-count", [
element(
"a",
[text(String(count))],
[
{ name: "href", value: `${href}#linked-from` },
{ name: "aria-label", value: `${count} pages link here` },
]
),
]),
]);
},
Strong: ({ content }) =>
element(
"strong",
content.map((child) => renderInline(child, counts, pagePath))
),
Text: ({ value }) =>
text(value.replaceAll(/(?<=\p{L})\u0027(?=\p{L})/gu, "’")),
});

const missingSnippet = (message: string) =>
element(
"p",
[text(message)],
[
{ name: "class", value: "reader-missing" },
{ name: "role", value: "note" },
]
);

const legacyNodes = (flags: ReaderPageFlags) => {
const { anchors = [], inboundCounts = {} } =
Option.getOrUndefined(flags.references) ?? {};

const { path, sourcePath } = flags.page;

const paragraph = (
tag: "li" | "p",
content: readonly ReaderInlineValue[]
) => {
const id = readerParagraphAnchor(tag, content, anchors);

const children = content.map((inline) =>
renderInline(inline, inboundCounts, path)
);

return id === undefined
? element(tag, children)
: attributed(tag, "id", id, [
...children,
element(
"a",
[text("¶")],
[
{ name: "href", value: `#${id}` },
{ name: "class", value: "paragraph-link" },
{ name: "aria-label", value: "Link to this paragraph" },
]
),
]);
};

const snippet = (html: string) =>
attributed(
"div",
"class",
"reader-snippet",
compileReaderSnippet(html, sourcePath)
);

const codeFence = (value: string) => {
const fence = flags.codeFences.find((entry) => entry.value === value);

return fence === undefined
? missingSnippet("This code block is not in the built page.")
: snippet(fence.html);
};

return flags.blocks.map((block) =>
ReaderBlock.match<ReaderNodeValue>(block, {
CodeFence: ({ value }) => codeFence(value),
CopyPrompt: ({ id }) => {
const control = ReaderNode.CopyPrompt({ id, primary: false });
const prompt = flags.copyPrompts.find((entry) => entry.id === id);

return prompt?.showText === true
? attributed("div", "class", "prompt", [
control,
attributed("details", "class", "prompt-text", [
element("summary", [text("See the prompt")]),
element("pre", [element("code", [text(prompt.text)])]),
]),
])
: control;
},
Diagram: ({ alt, value }) =>
element(
"figure",
[codeFence(value), element("figcaption", [text(alt)])],
[
{ name: "role", value: "img" },
{ name: "aria-label", value: alt },
]
),
Heading: ({ id, level, title }) =>
attributed(headingTags[level ?? 2], "id", id, [text(title)]),
List: ({ items }) =>
element(
"ul",
items.map((item) => paragraph("li", item))
),
Paragraph: ({ content }) => paragraph("p", content),
PromptText: ({ value }) =>
attributed("pre", "class", "prompt", [text(value)]),
Snippet: (reference) => {
const excerpt = flags.snippets.find(
(entry) =>
entry.repo === reference.repo &&
entry.path === reference.path &&
entry.at === reference.at &&
entry.lines === reference.lines
);

return excerpt === undefined
? missingSnippet(
`The excerpt ${reference.path} lines ${reference.lines} is not in the built page.`
)
: snippet(excerpt.html);
},
})
);
};

export const compileLegacyReaderPage = Effect.fn("reader.compileLegacyPage")(
function* compileLegacyReaderPage(flags: ReaderPageFlags) {
const { path, sourcePath } = flags.page;

const blocks = yield* Effect.try({
catch: (cause) =>
new ReaderInputError({
cause,
message: `Could not project the reader blocks for ${path}. Fix: check the excerpt tags and attributes in \`reader-body-document.ts\`.`,
sourcePath,
}),
try: () => legacyNodes(flags),
});

const body = Option.getOrElse(flags.bodyNodes, () => [
element("h1", [text(flags.heading)]),
]);

return {
...flags,
blocks: [],
bodyNodes: Option.some([...body, ...blocks]),
};
}
);
8 changes: 8 additions & 0 deletions apps/web/src/client/reader-copy-prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Schema } from "effect";

export const CopyPrompt = Schema.Struct({
id: Schema.String,
label: Schema.String,
showText: Schema.Boolean,
text: Schema.String,
});
44 changes: 44 additions & 0 deletions apps/web/src/client/reader-effect-ui/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Command } from "@effect/ui";
import { Effect, Schema } from "effect";

import { ReaderMessage } from "./model.js";

export const DetectClipboard = Command.make(
"reader/DetectClipboard",
{ args: {} },
() =>
Effect.sync(() =>
ReaderMessage.CompletedDetectClipboard({
available: navigator.clipboard !== undefined,
})
)
);

class ClipboardFailure extends Schema.TaggedError<
ClipboardFailure,
{ readonly brand: unique symbol }
>()("ClipboardFailure", { cause: Schema.Unknown }) {}

export const CopyReaderText = Command.make(
"reader/CopyReaderText",
{ args: { id: Schema.String, text: Schema.String }, mode: "concurrent" },
({ id, text }) =>
Effect.tryPromise({
catch: (cause) => new ClipboardFailure({ cause }),
try: () => navigator.clipboard.writeText(text),
}).pipe(
Effect.match({
onFailure: () => ReaderMessage.FailedCopyReaderText({ id }),
onSuccess: () => ReaderMessage.SucceededCopyReaderText({ id }),
})
)
);

export const WaitBeforeCopyReset = Command.make(
"reader/WaitBeforeCopyReset",
{ args: { id: Schema.String }, mode: "concurrent" },
({ id }) =>
Effect.sleep("2 seconds").pipe(
Effect.as(ReaderMessage.CompletedWaitBeforeCopyReset({ id }))
)
);
69 changes: 69 additions & 0 deletions apps/web/src/client/reader-effect-ui/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Html } from "@effect/ui/html";
import { Record } from "effect";

import { view } from "../../features/reader-effect-ui/view.js";
import {
CopyReaderText,
DetectClipboard,
WaitBeforeCopyReset,
} from "./command.js";
import {
findCopyPrompt,
ReaderMessage,
ReaderModel,
ReaderProps,
} from "./model.js";

export const ReaderPage = Html.component({
name: "ReaderPage",
props: ReaderProps,
model: ReaderModel,
message: ReaderMessage,
}).make({
init: ($) => {
const initial = $.set({ clipboard: "unknown", copyStatuses: {} });

return $.props.copyPrompts.length === 0
? initial
: initial.run(DetectClipboard.make({}));
},
update: ($, message) =>
ReaderMessage.match(message, {
ClickedCopy: ({ id }) => {
const status = $.model.copyStatuses[id];
const prompt = findCopyPrompt($.props, id);

if (
$.model.clipboard !== "available" ||
status === "copying" ||
status === "copied" ||
prompt === undefined
) {
return $.model;
}

return $.evolve({
copyStatuses: Record.set($.model.copyStatuses, id, "copying"),
}).run(CopyReaderText.make({ id, text: prompt.text }));
},
CompletedDetectClipboard: ({ available }) =>
$.evolve({ clipboard: available ? "available" : "unavailable" }),
CompletedWaitBeforeCopyReset: ({ id }) =>
$.model.copyStatuses[id] === "copied"
? $.evolve({ copyStatuses: Record.remove(id) })
: $.model,
FailedCopyReaderText: ({ id }) =>
$.model.copyStatuses[id] === "copying"
? $.evolve({
copyStatuses: Record.set($.model.copyStatuses, id, "failed"),
})
: $.model,
SucceededCopyReaderText: ({ id }) =>
$.model.copyStatuses[id] === "copying"
? $.evolve({
copyStatuses: Record.set($.model.copyStatuses, id, "copied"),
}).run(WaitBeforeCopyReset.make({ id }))
: $.model,
}),
view: (h, model) => view(h, h.props, model),
});
11 changes: 11 additions & 0 deletions apps/web/src/client/reader-effect-ui/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Effect } from "effect";

import { installReaderTools } from "../web-mcp-boot.js";
import { hydrateReader } from "./hydration.js";

import "../../../../site/src/rat.css";
import "../reader.css";

installReaderTools();

Effect.runFork(hydrateReader(document));
Loading
Loading