组件示例 CODE PREVIEW · SHIKI
代码高亮与预览.
检查语法着色、片段标记、行号和执行行的展示。
参考 code-pc 的 CodeMark 和 PreviewEditor。所有算法与数据结构页面共用此组件;可在这里检查主题、行高亮和片段标记效果。
// 相邻比较与交换function bubbleSort(arr) { for (let end = arr.length - 1; end > 0; end--) { for (let j = 0; j < end; j++) { if (arr[j] > arr[j + 1]) { [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; } } } return arr;}const numbers = [42, 18, 65, 30];console.log(bubbleSort(numbers));行号从 1 开始;activeLines 使用从 0 开始的索引。复制代码会去掉已解析的 @@ 标记。
预览设置
编辑示例代码
可以加入空行、长行、跨 token 的 @@片段@@,检查预览效果。修改语言会恢复该语言的示例。
<CodePreview code={source} language="javascript" theme="tokyoCity" activeLines={[4, 5]} marks={[{ index: 0, bg: '#ffd84d', color: '#26201a' }]} lineNumbers followActiveLine maxHeight={360}/>