Skip to content

Commit a26beaa

Browse files
authored
Merge pull request #99 from devlive-community/refactor-nodejs
fix: 修复代码片段语法高亮
2 parents ddc76c0 + 9e67a18 commit a26beaa

4 files changed

Lines changed: 137 additions & 5 deletions

File tree

backend/lib/extension/marked/code-block.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const CodeBlockExtension = {
5555
if (match) {
5656
let language = match[1].toLowerCase();
5757

58-
// Parse showLineNumbers and title from the full match string
58+
// 在函数开始就解析所有选项
5959
const showLineNumbers = match[0].includes('showLineNumbers') &&
6060
(!match[0].includes('showLineNumbers=false'));
6161

@@ -71,9 +71,18 @@ const CodeBlockExtension = {
7171
'bash': 'bash',
7272
'shell': 'bash',
7373
'sh': 'bash',
74-
'vue': 'javascript', // Add Vue support
74+
'vue': 'javascript',
7575
'jsx': 'jsx',
76-
'tsx': 'tsx'
76+
'tsx': 'tsx',
77+
'java': 'java',
78+
'py': 'python',
79+
'python': 'python',
80+
'cpp': 'cpp',
81+
'c++': 'cpp',
82+
'cs': 'csharp',
83+
'c#': 'csharp',
84+
'ts': 'typescript',
85+
'typescript': 'typescript'
7786
};
7887

7988
language = languageMap[language] || language;
@@ -100,6 +109,7 @@ const CodeBlockExtension = {
100109
console.warn(`Failed to highlight code for language: ${language}`, e);
101110
}
102111

112+
// 返回未高亮的版本,确保所有变量都已定义
103113
return {
104114
type: 'infosphereCodeBlock',
105115
raw: match[0],

backend/middleware/socket-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function initializeSocketHandlers(io) {
144144

145145
// 如果需要预览,渲染并返回
146146
if (data.needPreview) {
147-
const html = `<script src='https://cdn.tailwindcss.com'></script>` + marked.parse(data.content || '')
147+
const html = `<link href="https://cdnjs.cloudflare.com/ajax/libs/prism-themes/1.9.0/prism-ghcolors.css" rel="stylesheet"/><script src='https://cdn.tailwindcss.com'></script>` + marked.parse(data.content || '')
148148
socket.emit('document-preview-updated', {
149149
documentSlug: data.documentSlug,
150150
html: html

backend/routes/book.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ router.get('/reader/:username/:book_slug/:docs_slug?', asyncHandler(async (req,
116116

117117
document.html = isEmpty(document.content)
118118
? undefined
119-
: `<script src='https://cdn.tailwindcss.com'></script>` + marked.parse(document.content || '')
119+
: `<link href="https://cdnjs.cloudflare.com/ajax/libs/prism-themes/1.9.0/prism-ghcolors.css" rel="stylesheet"/><script src='https://cdn.tailwindcss.com'></script>` + marked.parse(document.content || '')
120120

121121
res.render('pages/document/reader', {
122122
book,

frontend/public/css/prism.min.css

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* GHColors theme by Avi Aryan (http://aviaryan.in)
3+
* Inspired by Github syntax coloring
4+
*/
5+
6+
code[class*="language-"],
7+
pre[class*="language-"] {
8+
color: #393A34;
9+
font-family: "Consolas", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace;
10+
direction: ltr;
11+
text-align: left;
12+
white-space: pre;
13+
word-spacing: normal;
14+
word-break: normal;
15+
font-size: .9em;
16+
line-height: 1.2em;
17+
18+
-moz-tab-size: 4;
19+
-o-tab-size: 4;
20+
tab-size: 4;
21+
22+
-webkit-hyphens: none;
23+
-moz-hyphens: none;
24+
-ms-hyphens: none;
25+
hyphens: none;
26+
}
27+
28+
pre > code[class*="language-"] {
29+
font-size: 1em;
30+
}
31+
32+
pre[class*="language-"]::-moz-selection, pre[class*="language-"] ::-moz-selection,
33+
code[class*="language-"]::-moz-selection, code[class*="language-"] ::-moz-selection {
34+
background: #b3d4fc;
35+
}
36+
37+
pre[class*="language-"]::selection, pre[class*="language-"] ::selection,
38+
code[class*="language-"]::selection, code[class*="language-"] ::selection {
39+
background: #b3d4fc;
40+
}
41+
42+
/* Code blocks */
43+
pre[class*="language-"] {
44+
padding: 1em;
45+
margin: .5em 0;
46+
overflow: auto;
47+
border: 1px solid #dddddd;
48+
background-color: white;
49+
}
50+
51+
/* Inline code */
52+
:not(pre) > code[class*="language-"] {
53+
padding: .2em;
54+
padding-top: 1px;
55+
padding-bottom: 1px;
56+
background: #f8f8f8;
57+
border: 1px solid #dddddd;
58+
}
59+
60+
.token.comment,
61+
.token.prolog,
62+
.token.doctype,
63+
.token.cdata {
64+
color: #999988;
65+
font-style: italic;
66+
}
67+
68+
.token.namespace {
69+
opacity: .7;
70+
}
71+
72+
.token.string,
73+
.token.attr-value {
74+
color: #e3116c;
75+
}
76+
77+
.token.punctuation,
78+
.token.operator {
79+
color: #393A34; /* no highlight */
80+
}
81+
82+
.token.entity,
83+
.token.url,
84+
.token.symbol,
85+
.token.number,
86+
.token.boolean,
87+
.token.variable,
88+
.token.constant,
89+
.token.property,
90+
.token.regex,
91+
.token.inserted {
92+
color: #36acaa;
93+
}
94+
95+
.token.atrule,
96+
.token.keyword,
97+
.token.attr-name,
98+
.language-autohotkey .token.selector {
99+
color: #00a4db;
100+
}
101+
102+
.token.function,
103+
.token.deleted,
104+
.language-autohotkey .token.tag {
105+
color: #9a050f;
106+
}
107+
108+
.token.tag,
109+
.token.selector,
110+
.language-autohotkey .token.keyword {
111+
color: #00009f;
112+
}
113+
114+
.token.important,
115+
.token.function,
116+
.token.bold {
117+
font-weight: bold;
118+
}
119+
120+
.token.italic {
121+
font-style: italic;
122+
}

0 commit comments

Comments
 (0)