xfy 763edc300d feat(editor): NodeView 适配 onRunCode 协议(extractLang + overridesJson 提取)
runCode 改传纯语言名(extractLang) + overrides JSON 字符串(extractOverridesJson),
对齐 Task 4 Rust 桥接 RunCodeOptsJs(language/source/overrides_json):
- highlight.ts: 新增 extractOverridesJson,对齐后端 parse_fence_info 取以 { 开头的 token
- code-block-view.ts: RunCodeOpts 加 overridesJson 字段,runCode 改传 extractLang/extractOverridesJson 提取结果
- index.ts: EditorOptions.onRunCode 类型同步加 overridesJson
- 测试: extractOverridesJson 6 例 + code-block-view 点击断言更新(纯语言名 + overridesJson)
2026-07-06 18:17:33 +08:00

47 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { common, createLowlight } from 'lowlight';
const base = createLowlight(common);
/**
* 从完整 fence info string 提取语言名(首个 token小写化
*
* 与后端 parse_fence_infosrc/api/code_runner/languages.rs:85对齐
* info string 形如 `python runnable {"timeout_secs":10}`,语言是首个空白分隔的 token
* 再 to_lowercaselowlight 注册名均为小写,大写会导致 Unknown language 抛错)。
*/
export function extractLang(info: string): string {
const first = info.trim().split(/\s+/)[0];
return first ? first.toLowerCase() : '';
}
/**
* 从完整 fence info string 提取 overrides JSON 字符串。
*
* 与后端 parse_fence_infosrc/api/code_runner/languages.rs:93-97对齐
* info string 形如 `python runnable {"timeout_secs":10}`,提取以 `{` 开头的 token。
* 无 overrides 时返回空串Rust 侧空串视为 None
*/
export function extractOverridesJson(info: string): string {
const token = info
.trim()
.split(/\s+/)
.find((t) => t.startsWith('{'));
return token ?? '';
}
/**
* lowlight 实例,包装 highlight 方法以处理完整 info string。
*
* CodeBlockLowlight 取 `block.node.attrs.language`(如 `python runnable {...}`
* 直接调 `lowlight.highlight(language, code)`,但 lowlight 只认 `python` 这个 token。
* wrapper 先 extractLang 提取首 token再高亮使 runnable 块也能按正确语言着色。
*
* 普通 code blocklanguage='python'也兼容extractLang('python') → 'python'。
*/
export const lowlight = {
...base,
highlight(language: string, value: string) {
return base.highlight(extractLang(language), value);
},
};