✨ feat: 添加 Sarasa Mono 字体支持并优化样式
This commit is contained in:
@@ -36,7 +36,18 @@
|
||||
return scriptSrc.replace(/\/include\/vditor-adapter\.js(?:[?#].*)?$/, '/ace');
|
||||
}
|
||||
|
||||
function getLocalSarasaFontUrl() {
|
||||
var scriptSrc = currentScriptRef && currentScriptRef.src;
|
||||
|
||||
if (!scriptSrc) {
|
||||
return 'template/syzoj/fonts/SarasaMonoSC-Regular.ttf';
|
||||
}
|
||||
|
||||
return scriptSrc.replace(/\/include\/vditor-adapter\.js(?:[?#].*)?$/, '/template/syzoj/fonts/SarasaMonoSC-Regular.ttf');
|
||||
}
|
||||
|
||||
function injectAdapterStyle() {
|
||||
var sarasaFontUrl = getLocalSarasaFontUrl();
|
||||
var style;
|
||||
|
||||
if (styleInjected) {
|
||||
@@ -47,13 +58,19 @@
|
||||
style.type = 'text/css';
|
||||
style.setAttribute('data-hustoj-vditor', 'adapter-style');
|
||||
style.textContent = [
|
||||
'@font-face{font-family:"Sarasa Mono SC Local";src:local("等距更纱黑体 SC"),local("Sarasa Mono SC"),url("' + sarasaFontUrl + '") format("truetype");font-weight:400;font-style:normal;font-display:swap;}',
|
||||
'.hustoj-vditor-hidden{display:none !important;}',
|
||||
'.hustoj-vditor-wrapper{width:100%;margin:0 0 12px;}',
|
||||
'.hustoj-vditor-code{--hustoj-vditor-font-size:18px;text-align:left;}',
|
||||
'.hustoj-vditor-wrapper,.hustoj-vditor-preview,.hustoj-vditor-markdown,.hustoj-vditor-code{--hustoj-vditor-font-family:"Sarasa Mono SC Local","等距更纱黑体 SC","Sarasa Mono SC","Sarasa Fixed SC","Sarasa Gothic SC",Consolas,"Cascadia Mono","JetBrains Mono","Courier New",monospace;--hustoj-vditor-font-size:24px;--hustoj-vditor-preview-code-font-size:24px;}',
|
||||
'.hustoj-vditor-markdown,.hustoj-vditor-markdown .vditor,.hustoj-vditor-markdown .vditor-content,.hustoj-vditor-markdown .vditor-ir,.hustoj-vditor-markdown .vditor-sv,.hustoj-vditor-markdown .vditor-reset,.hustoj-vditor-markdown .vditor-wysiwyg,.hustoj-vditor-markdown .vditor-sv__preview{text-align:left !important;}',
|
||||
'.hustoj-vditor-markdown .vditor{margin-left:0 !important;margin-right:auto !important;}',
|
||||
'.hustoj-vditor-markdown .vditor-toolbar{justify-content:flex-start;}',
|
||||
'.hustoj-vditor-markdown .vditor-content,.hustoj-vditor-markdown .vditor-reset,.hustoj-vditor-markdown .vditor-reset *,.hustoj-vditor-markdown textarea{font-family:var(--hustoj-vditor-font-family) !important;}',
|
||||
'.hustoj-vditor-code{text-align:left;}',
|
||||
'.hustoj-vditor-code.ace_editor{position:relative;border:1px solid #d4d4d5;border-radius:6px;line-height:1.5;}',
|
||||
'.hustoj-vditor-code .ace_scroller,',
|
||||
'.hustoj-vditor-code .ace_gutter,',
|
||||
'.hustoj-vditor-code .ace_content{font-family:Consolas,Monaco,"Courier New",monospace !important;font-size:var(--hustoj-vditor-font-size) !important;line-height:1.5 !important;}',
|
||||
'.hustoj-vditor-code .ace_content{font-family:var(--hustoj-vditor-font-family) !important;font-size:var(--hustoj-vditor-font-size) !important;line-height:1.5 !important;}',
|
||||
'.hustoj-vditor-code .ace_gutter{background:#f8f8f8;color:#8a8a8a;}',
|
||||
'.hustoj-vditor-dark.ace_editor{background:#272822;border-color:#4a4a4a;}',
|
||||
'.hustoj-vditor-dark .ace_gutter{background:#2f3129;color:#bcbcbc;}',
|
||||
@@ -63,7 +80,7 @@
|
||||
'.hustoj-vditor-code .vditor-reset,',
|
||||
'.hustoj-vditor-code .vditor-content,',
|
||||
'.hustoj-vditor-code .vditor,',
|
||||
'.hustoj-vditor-code textarea{text-align:left !important;font-family:Consolas,Monaco,"Courier New",monospace !important;font-size:var(--hustoj-vditor-font-size) !important;line-height:1.5 !important;}',
|
||||
'.hustoj-vditor-code textarea{text-align:left !important;font-family:var(--hustoj-vditor-font-family) !important;font-size:var(--hustoj-vditor-font-size) !important;line-height:1.5 !important;}',
|
||||
'.hustoj-vditor-code .vditor-sv,',
|
||||
'.hustoj-vditor-code .vditor-ir{min-height:100%;}',
|
||||
'.hustoj-vditor-dark .vditor{background:#272822;border-color:#4a4a4a;}',
|
||||
@@ -74,11 +91,13 @@
|
||||
'.hustoj-vditor-mail .vditor-toolbar{border-bottom:1px solid #ddd;}',
|
||||
'.hustoj-vditor-mail .vditor{height:100%;min-height:180px;}',
|
||||
'.hustoj-vditor-preview .vditor-reset pre{position:relative;margin:16px 0;padding:16px 18px;border:1px solid #cfd7e6;border-radius:12px;background:linear-gradient(180deg,#f9fbff 0%,#f3f7ff 100%);box-shadow:0 8px 20px rgba(15,23,42,0.06);}',
|
||||
'.hustoj-vditor-preview .vditor-reset pre code{display:block;font-size:15px;line-height:1.7;}',
|
||||
'.hustoj-vditor-preview .vditor-copy{display:block !important;position:absolute;top:12px;right:12px;z-index:2;}',
|
||||
'.hustoj-vditor-preview .vditor-copy span{display:inline-flex;align-items:center;gap:6px;padding:6px 10px;border:1px solid #c8d3e6;border-radius:999px;background:rgba(255,255,255,0.96);box-shadow:0 4px 12px rgba(15,23,42,0.08);font-size:12px;font-weight:600;color:#2f4f7f;}',
|
||||
'.hustoj-vditor-preview .vditor-reset pre code{display:block;font-family:var(--hustoj-vditor-font-family) !important;font-size:var(--hustoj-vditor-preview-code-font-size) !important;line-height:1.7;}',
|
||||
'.hustoj-vditor-preview .vditor-reset pre div.vditor-copy{display:block !important;opacity:1 !important;visibility:visible !important;position:absolute;top:12px;right:12px;z-index:2;}',
|
||||
'.hustoj-vditor-preview .vditor-copy span{display:inline-flex;align-items:center;gap:6px;padding:6px 10px;border:1px solid #c8d3e6;border-radius:999px;background:rgba(255,255,255,0.96);box-shadow:0 4px 12px rgba(15,23,42,0.08);font-size:12px;font-weight:600;color:#2f4f7f;position:static !important;}',
|
||||
'.hustoj-vditor-preview .vditor-copy span::after{content:attr(aria-label);}',
|
||||
'.hustoj-vditor-preview .vditor-copy svg{width:14px !important;height:14px !important;color:currentColor;}',
|
||||
'.hustoj-vditor-preview .hustoj-vditor-sample-heading{display:flex;align-items:center;margin:24px 0 0;padding:12px 16px;border:1px solid #d6dde8;border-bottom:0;border-radius:14px 14px 0 0;background:#eef4ff;color:#274061;font-size:1.05em;font-weight:700;line-height:1.4;}',
|
||||
'.hustoj-vditor-preview .vditor-reset pre.hustoj-vditor-sample-box{margin-top:0;border-top-left-radius:0;border-top-right-radius:0;border-top-color:#d6dde8;padding-top:52px;}',
|
||||
'.hustoj-vditor-preview .vditor-reset .language-mermaid,',
|
||||
'.hustoj-vditor-preview .vditor-reset .language-flowchart{margin:18px 0;padding:14px;border:1px solid #d7dce5;border-radius:12px;background:#fff;overflow:auto;text-align:center;}',
|
||||
'.hustoj-vditor-preview .vditor-reset .language-mermaid svg,',
|
||||
@@ -466,6 +485,8 @@
|
||||
|
||||
element.classList.add('hustoj-vditor-preview');
|
||||
|
||||
decorateSampleBlocks(element);
|
||||
|
||||
Array.prototype.forEach.call(
|
||||
element.querySelectorAll('.language-mermaid, .language-flowchart'),
|
||||
function (diagramContainer) {
|
||||
@@ -497,6 +518,73 @@
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeHeadingText(text) {
|
||||
return String(text || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function parseSampleHeading(text) {
|
||||
var normalized = normalizeHeadingText(text).replace(/[::]+$/, '');
|
||||
var match = normalized.match(/^(?:(?:样例|示例|sample|example)\s*)?(输入|输出|input|output)\s*([0-9]+)?$/i);
|
||||
var kind;
|
||||
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
|
||||
kind = match[1].toLowerCase();
|
||||
if (kind === '输入' || kind === 'input') {
|
||||
kind = 'input';
|
||||
} else {
|
||||
kind = 'output';
|
||||
}
|
||||
|
||||
return {
|
||||
kind: kind,
|
||||
index: match[2] || ''
|
||||
};
|
||||
}
|
||||
|
||||
function getSiblingCodeBlock(heading) {
|
||||
var sibling = heading ? heading.nextElementSibling : null;
|
||||
|
||||
while (sibling && sibling.tagName === 'P' && !normalizeHeadingText(sibling.textContent)) {
|
||||
sibling = sibling.nextElementSibling;
|
||||
}
|
||||
|
||||
if (!sibling || sibling.tagName !== 'PRE') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sibling;
|
||||
}
|
||||
|
||||
function decorateSampleBlocks(element) {
|
||||
Array.prototype.forEach.call(element.querySelectorAll('h1, h2, h3, h4, h5, h6'), function (heading) {
|
||||
var sampleMeta = parseSampleHeading(heading.textContent);
|
||||
var codeBlock;
|
||||
|
||||
if (!sampleMeta) {
|
||||
return;
|
||||
}
|
||||
|
||||
codeBlock = getSiblingCodeBlock(heading);
|
||||
if (!codeBlock) {
|
||||
return;
|
||||
}
|
||||
|
||||
heading.classList.add('hustoj-vditor-sample-heading');
|
||||
heading.setAttribute('data-hustoj-sample-kind', sampleMeta.kind);
|
||||
if (sampleMeta.index) {
|
||||
heading.setAttribute('data-hustoj-sample-index', sampleMeta.index);
|
||||
}
|
||||
codeBlock.classList.add('hustoj-vditor-sample-box');
|
||||
codeBlock.setAttribute('data-hustoj-sample-kind', sampleMeta.kind);
|
||||
if (sampleMeta.index) {
|
||||
codeBlock.setAttribute('data-hustoj-sample-index', sampleMeta.index);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function renderMarkdownBlocks(target, options) {
|
||||
var renderOptions = options || {};
|
||||
|
||||
@@ -732,7 +820,7 @@
|
||||
|
||||
state = {
|
||||
theme: options.theme || 'ace/theme/chrome',
|
||||
fontSize: parseInt(options.fontSize, 10) || 18,
|
||||
fontSize: parseInt(options.fontSize, 10) || 24,
|
||||
mode: normalizeMode(options.mode || ''),
|
||||
wrap: true
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user