Skip to content

XCode — Code Display ​

A code display component with syntax highlighting. Ships with a hand-written, zero-dependency lightweight highlighter (bundled in XCode/highlight.js, no npm install needed). Two dimensions are distinguished:

  • Language label colors: JS / Vue / Bash / JSON … different languages get different label colors.
  • Semantic status badges: default / success / warning / error / info — great for marking "correct example / wrong example / hint".

Also includes line numbers, one-click copy, and collapsible sections, and adapts to light / dark themes.

Live Demo ​

avg.jsJAVASCRIPT 代码
1// Compute average
2const avg = (list) =>
3 list.reduce((a, b) => a + b, 0) / list.length
4
5console.log(avg([1, 2, 3, 4])) // 2.5
avg.jsJAVASCRIPT Recommended
1// Compute average
2const avg = (list) =>
3 list.reduce((a, b) => a + b, 0) / list.length
4
5console.log(avg([1, 2, 3, 4])) // 2.5
avg.jsJAVASCRIPT 警告
1// Compute average
2const avg = (list) =>
3 list.reduce((a, b) => a + b, 0) / list.length
4
5console.log(avg([1, 2, 3, 4])) // 2.5
vue
<XCode title="avg.js" language="javascript" :code="jsCode" />
<XCode title="avg.js" language="javascript" :code="jsCode" status="success" statusText="Recommended" />

Props ​

PropTypeDefaultDescription
codeString''Source code to display (required)
languageString'javascript'Highlight language (also used as the label): js/ts/html/vue/svg/css/scss/json/bash/shell/sh/console/md/py/yaml and aliases
titleString''Title-bar file name (hidden when omitted)
statusString'default'Semantic status: default/success/warning/error/info
statusTextString''Custom text for the status badge
showLineNumbersBooleantrueWhether to show line numbers
collapsibleBooleanfalseWhether collapsible (shows only the first previewLines lines)
previewLinesNumber8Number of lines shown when collapsed
maxHeightString'420px'Max height of the code body (scrolls when exceeded)
copyableBooleantrueWhether to show the copy button

v-model ​

NameTypeDescription
collapsedBooleanCollapsed state (v-model:collapsed)

Status Reference ​

statusColorDefault textUse case
defaultgray #909399CodeOrdinary example
successgreen #2ba471SuccessRecommended / correct example
warningorange #e6a23cWarningCaveats / deprecated usage
errorred #f56c6cErrorForbidden / erroneous example
infoblue #409effHintSupplementary note

Usage Notes ​

  • Highlighting is provided by the component's own XCode/highlight.js + XCode/highlight.css. Copy the whole XCode/ folder to use it — the styles are self-contained (no missing CSS) and do not depend on your app shell variables or any UI library.
  • Built-in languages: javascript/ts/xml(html,vue,svg)/css/scss/json/bash(sh,shell,zsh,console)/markdown(md)/python(py)/yaml(yml). Unknown languages automatically fall back to plain-text escaping.
  • To add a language, add lexer rules in XCode/highlight.js and hook them into the highlightCode switch; to change colors, edit XCode/highlight.css.

基于 VitePress + Vue 3 构建