Files
usaco/site/web/src/components/CodeBlock.vue
T
adamfandClaude Opus 5.5 4dc8d1674c
Test and publish the site / test (push) Successful in 1m10s
Test and publish the site / publish (push) Successful in 1m29s
Tidy the site's problem pages and typeset math in explanations
The problem page's Explain and recording-review forms took most of the
first screen, above the solution. They now fold into the admin bar with
the editor, one open at a time; a running job is reported in the bar,
so a form can be closed while it works.

The problem list is grouped by month when sorted by recency, as one
card per month with divided rows, laid out on a fixed grid so rows
line up on a phone. Empty status tabs are hidden.

Rust solutions are now highlighted, the file picker is styled like the
other buttons, header links no longer break mid-word on a phone, and
keyboard focus is visible.

Markdown everywhere now goes through one renderer that typesets
$...$, $$...$$ and ```math with KaTeX. The explanation prompt asked for
$...$ only when the statement used it, so formulas came out as inline
code; it now asks for LaTeX for math and code spans for code, and the
review prompt does the same for complexities.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-20 02:19:24 +01:00

112 lines
2.9 KiB
Vue

<script setup>
/**
* A file with line numbers, highlighting and a copy button. Only the
* languages the workspace uses are registered, which keeps highlight.js to a
* few kilobytes instead of the full bundle.
*/
import { computed, ref } from "vue";
import hljs from "highlight.js/lib/core";
import cpp from "highlight.js/lib/languages/cpp";
import python from "highlight.js/lib/languages/python";
import rust from "highlight.js/lib/languages/rust";
hljs.registerLanguage("cpp", cpp);
hljs.registerLanguage("c", cpp);
hljs.registerLanguage("python", python);
hljs.registerLanguage("rust", rust);
const props = defineProps({
content: { type: String, required: true },
lang: { type: String, default: "" },
truncated: { type: Boolean, default: false },
});
// highlight.js escapes the source itself, so v-html on its output is safe.
const html = computed(() => {
const text = props.content.replace(/\n$/, "");
if (props.lang && hljs.getLanguage(props.lang)) {
return hljs.highlight(text, { language: props.lang }).value;
}
return text.replace(/[&<>]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;" })[c]);
});
const lines = computed(() => props.content.replace(/\n$/, "").split("\n").length);
const copied = ref(false);
async function copy() {
try {
await navigator.clipboard.writeText(props.content);
copied.value = true;
setTimeout(() => (copied.value = false), 1500);
} catch {
// Clipboard access refused (insecure context or permissions): nothing to do.
}
}
</script>
<template>
<div class="code">
<button class="copy" type="button" @click="copy">{{ copied ? "Copied" : "Copy" }}</button>
<div class="scroll">
<pre class="gutter" aria-hidden="true">{{ Array.from({ length: lines }, (_, i) => i + 1).join("\n") }}</pre>
<pre class="hljs src"><code v-html="html"></code></pre>
</div>
<p v-if="truncated" class="cut muted">Cut short: the file is larger than the site shows.</p>
</div>
</template>
<style scoped>
.code {
position: relative;
background: var(--color-paper);
border: 1px solid var(--color-line-soft);
border-radius: 6px;
}
.scroll {
display: flex;
overflow-x: auto;
font-family: var(--font-mono);
font-size: 0.8125rem;
line-height: 1.55;
}
pre {
margin: 0;
padding: 12px 0;
font-family: inherit;
}
.gutter {
padding: 12px 10px 12px 12px;
text-align: right;
color: var(--color-ink-faint);
user-select: none;
border-right: 1px solid var(--color-line-soft);
position: sticky;
left: 0;
background: var(--color-paper);
}
.src {
padding-left: 12px;
padding-right: 64px;
}
.copy {
position: absolute;
top: 8px;
right: 8px;
z-index: 1;
padding: 2px 10px;
font-size: 0.75rem;
border: 1px solid var(--color-line);
border-radius: 4px;
background: var(--color-paper);
color: var(--color-ink-muted);
cursor: pointer;
}
.copy:hover {
color: var(--color-ink);
}
.cut {
padding: 6px 12px;
font-size: 0.8rem;
border-top: 1px solid var(--color-line-soft);
}
</style>