Add an Explain button that has Claude explain a problem
Test and publish the site / publish (push) Successful in 8m15s
Test and publish the site / test (push) Successful in 5m51s

When stuck, the admin can ask Claude for an intuitive explanation of a
problem. It is committed as explain.md and shown as folded sections, so
the reader opens only as much as they need: the key observation, hints
that get stronger, the approach, a walkthrough with an SVG diagram per
step, complexity, pitfalls and a pseudocode sketch.

- internal/claude: streamed call with a JSON-schema reply, high effort,
  the server-side refusal fallback, fixed error messages and logged
  token usage. CLAUDE_API_KEY and CLAUDE_MODEL (default claude-opus-5-5);
  without a key only this feature is off.
- internal/explain: the prompt, the schema, rendering to markdown, and
  a per-problem job tracker. The request starts a job and the page polls,
  since a call outlasts nginx's 120s timeout.
- internal/statement: fetches statements from LeetCode's GraphQL API and
  USACO's problem pages. Others (Codeforces is behind Cloudflare) are
  pasted.
- problem.md holds the statement, editable from the meta editor and
  shown only to the admin. Diagrams are sanitized with DOMPurify.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-19 20:38:15 +01:00
co-authored by Claude Opus 5.5
parent f0e10d2a58
commit 995b44a57a
27 changed files with 2144 additions and 67 deletions
+96
View File
@@ -0,0 +1,96 @@
<script setup>
/**
* explain.md, laid out as the ladder it was written to be: every "## "
* section folded away so the reader opens only as much as they need (the
* observation, then one hint at a time, ...). The line above the first
* section (who wrote it, when) stays visible.
*
* Markdown is rendered with html: false like the notes. The one exception
* is ```svg fences, the walkthrough's diagrams: they are inserted as SVG
* after DOMPurify's SVG profile has stripped scripts, event handlers and
* anything else that is not drawing. Claude wrote them, the admin can edit
* them, and the public sees them once the problem is solved.
*/
import { computed } from "vue";
import MarkdownIt from "markdown-it";
import DOMPurify from "dompurify";
const props = defineProps({ source: { type: String, required: true } });
const md = new MarkdownIt({ html: false, linkify: true });
const fence = md.renderer.rules.fence;
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
const t = tokens[idx];
if (t.info.trim() !== "svg") return fence(tokens, idx, options, env, self);
const clean = DOMPurify.sanitize(t.content, { USE_PROFILES: { svg: true, svgFilters: true } });
return `<figure class="diagram">${clean}</figure>`;
};
// Split on "## " headings outside code fences.
const parts = computed(() => {
const intro = [];
const sections = [];
let inFence = false;
for (const line of props.source.split("\n")) {
if (line.startsWith("```")) inFence = !inFence;
if (!inFence && line.startsWith("## ")) {
sections.push({ title: line.slice(3).trim(), lines: [] });
} else {
(sections.length ? sections[sections.length - 1].lines : intro).push(line);
}
}
return {
intro: md.render(intro.join("\n")),
sections: sections.map((s) => ({ title: s.title, html: md.render(s.lines.join("\n")) })),
};
});
</script>
<template>
<div class="explanation">
<div v-if="parts.intro.trim()" class="intro muted prose" v-html="parts.intro"></div>
<details v-for="(s, i) in parts.sections" :key="i + s.title" class="step card">
<summary>{{ s.title }}</summary>
<div class="body prose" v-html="s.html"></div>
</details>
</div>
</template>
<style scoped>
.explanation {
display: grid;
gap: 8px;
min-width: 0;
}
.intro {
font-size: 0.85rem;
}
.step summary {
padding: 8px 14px;
cursor: pointer;
font-weight: bold;
}
.step[open] summary {
border-bottom: 1px solid var(--color-line-soft);
}
.body {
padding: 12px 16px 16px;
min-width: 0;
overflow-wrap: anywhere;
}
.body :deep(.diagram) {
margin: 12px 0;
padding: 12px;
border: 1px solid var(--color-line-soft);
border-radius: 4px;
background: var(--color-paper);
color: var(--color-ink);
}
.body :deep(.diagram svg) {
display: block;
width: 100%;
max-width: 640px;
height: auto;
margin: 0 auto;
}
</style>