1
2
3
4
> [!info] 这是标注的标题
> 这是一个标注块。
> 它支持 **Markdown**、[[内部链接|内部链接]] 和 [[插入文件|嵌入]]!
> ![[Engelbart.jpg]]

然而,当这些标注内容发布到Hexo博客时,无法正常显示。虽然Hexo本身支持类似的标注语法:

1
{% note class_name %} Content (md partial supported) {% endnote %}

但为了保持Obsidian的编辑体验,我们需要一个插件来解决这个问题。

解决方案

在Hexo博客的scripts目录下创建hexo-obsidian-tabs.js文件,并将以下内容复制到文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'use strict';

/**
* Hexo Obsidian Tabs Plugin
* Support Obsidian Tabs plugin syntax rendering
*/

const { marked } = require('marked');

// Match the HTML output of tabs code blocks after markdown rendering
// Hexo converts ```tabs to <pre><code class="language-tabs">...</code></pre>
const TABS_HTML_REGEX = /<pre[^>]*>\s*<code[^>]*class="[^"]*language-tabs[^"]*"[^>]*>([\s\S]*?)<\/code>\s*<\/pre>/g;

// Match tab header in HTML content
const TAB_HEADER_REGEX = /tab:\s*([^\n]+)/;

// Match code blocks within tab content
const CODE_BLOCK_REGEX = /&#96;&#96;&#96;(\w*)\n([\s\S]*?)&#96;&#96;&#96;/g;

function parseTabsHtml(content) {
return content.replace(TABS_HTML_REGEX, (match, codeContent) => {
// Decode HTML entities
let decodedContent = codeContent
.replace(/&#x2F;/g, '/')
.replace(/&#x27;/g, "'")
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&amp;/g, '&');

// First, restore code blocks that were encoded
// Convert &#96; back to ` for code blocks
decodedContent = decodedContent.replace(/&#96;/g, '`');

// Split by tab headers
const lines = decodedContent.split('\n');
const tabs = [];
let currentTab = null;
let currentContent = [];

for (const line of lines) {
const tabMatch = line.match(/^tab:\s*(.+)$/);

if (tabMatch) {
// Save previous tab
if (currentTab) {
tabs.push({
title: currentTab,
content: currentContent.join('\n').trim()
});
}
// Start new tab
currentTab = tabMatch[1].trim();
currentContent = [];
} else if (currentTab) {
// Collect current tab content
currentContent.push(line);
}
}

// Save last tab
if (currentTab) {
tabs.push({
title: currentTab,
content: currentContent.join('\n').trim()
});
}

if (tabs.length === 0) {
return match;
}

// Generate unique ID
const tabsId = 'obsidian-tabs-' + Math.random().toString(36).substr(2, 9);

// Build HTML
let html = `<div class="obsidian-tabs" id="${tabsId}">`;

// Tab buttons
html += '<div class="obsidian-tabs-nav">';
tabs.forEach((tab, index) => {
const activeClass = index === 0 ? ' active' : '';
html += `<button class="obsidian-tab-btn${activeClass}" data-tab="${tabsId}-tab-${index}">${escapeHtml(tab.title)}</button>`;
});
html += '</div>';

// Tab content - render markdown for each tab
html += '<div class="obsidian-tabs-content">';
tabs.forEach((tab, index) => {
const activeClass = index === 0 ? ' active' : '';
// Use hexo's markdown renderer
const renderedContent = hexo.render.renderSync({ text: tab.content, engine: 'markdown' });
html += `<div class="obsidian-tab-panel${activeClass}" id="${tabsId}-tab-${index}">${renderedContent}</div>`;
});
html += '</div>';

html += '</div>';

return html;
});
}

function escapeHtml(text) {
return text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}

// Register filter - process after markdown rendering
hexo.extend.filter.register('after_post_render', function(data) {
if (!data.content) return data;
data.content = parseTabsHtml(data.content);
return data;
}, 1); // Lower priority to run after markdown rendering

// Inject CSS
hexo.extend.injector.register('head_end', `
<style>
.obsidian-tabs {
margin: 1.5em 0;
border: 1px solid var(--tab-border-color, #e0e0e0);
border-radius: 8px;
overflow: hidden;
background: var(--tab-bg, #fafafa);
}

.obsidian-tabs-nav {
display: flex;
background: var(--tab-nav-bg, #f0f0f0);
border-bottom: 1px solid var(--tab-border-color, #e0e0e0);
overflow-x: auto;
scrollbar-width: none;
-ms-overflow-style: none;
}

.obsidian-tabs-nav::-webkit-scrollbar {
display: none;
}

.obsidian-tab-btn {
padding: 12px 20px;
border: none;
background: transparent;
color: var(--tab-text-color, #666);
font-size: 14px;
font-weight: 500;
cursor: pointer;
transition: all 0.2s ease;
white-space: nowrap;
border-bottom: 2px solid transparent;
margin-bottom: -1px;
}

.obsidian-tab-btn:hover {
color: var(--tab-hover-color, #333);
background: var(--tab-hover-bg, rgba(0,0,0,0.03));
}

.obsidian-tab-btn.active {
color: var(--tab-active-color, #448aff);
border-bottom-color: var(--tab-active-color, #448aff);
background: var(--tab-bg, #fafafa);
}

.obsidian-tabs-content {
padding: 20px;
background: var(--tab-bg, #fafafa);
}

.obsidian-tab-panel {
display: none;
animation: fadeIn 0.3s ease;
}

.obsidian-tab-panel.active {
display: block;
}

.obsidian-tab-panel > *:first-child {
margin-top: 0;
}

.obsidian-tab-panel > *:last-child {
margin-bottom: 0;
}

.obsidian-tab-panel pre {
margin: 1em 0;
border-radius: 6px;
}

.obsidian-tab-panel code {
font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
}

/* Fix code block background color inside tabs */
.obsidian-tab-panel pre code {
background: transparent !important;
}

.obsidian-tab-panel .highlight {
background: transparent !important;
}

.obsidian-tab-panel .highlight pre {
background: var(--code-bg, #f5f5f5) !important;
}

.obsidian-tab-panel figure.highlight {
background: var(--code-bg, #f5f5f5) !important;
}

.obsidian-tab-panel td.gutter {
background: var(--code-gutter-bg, #f0f0f0) !important;
}

.obsidian-tab-panel td.code {
background: var(--code-bg, #f5f5f5) !important;
}

@media (prefers-color-scheme: dark) {
.obsidian-tab-panel .highlight pre,
.obsidian-tab-panel figure.highlight {
--code-bg: #2d2d2d;
--code-gutter-bg: #252525;
}
}

@media (prefers-color-scheme: dark) {
.obsidian-tabs {
--tab-bg: #1e1e1e;
--tab-nav-bg: #252526;
--tab-border-color: #333;
--tab-text-color: #999;
--tab-hover-color: #ccc;
--tab-hover-bg: rgba(255,255,255,0.05);
--tab-active-color: #58a6ff;
}
}

@keyframes fadeIn {
from { opacity: 0; transform: translateY(5px); }
to { opacity: 1; transform: translateY(0); }
}
</style>
`);

// Inject JavaScript
hexo.extend.injector.register('body_end', `
<script>
(function() {
document.querySelectorAll('.obsidian-tab-btn').forEach(function(btn) {
btn.addEventListener('click', function() {
var tabsContainer = this.closest('.obsidian-tabs');
var tabId = this.getAttribute('data-tab');

tabsContainer.querySelectorAll('.obsidian-tab-btn').forEach(function(b) {
b.classList.remove('active');
});
tabsContainer.querySelectorAll('.obsidian-tab-panel').forEach(function(p) {
p.classList.remove('active');
});

this.classList.add('active');
document.getElementById(tabId).classList.add('active');
});
});
})();
</script>
`);

console.log('[hexo-obsidian-tabs] Plugin loaded successfully!');

使用说明

  1. 将上述代码保存为hexo-obsidian-tabs.js文件
  2. 将该文件放置在Hexo博客的scripts目录下
  3. 重启Hexo服务器,插件将自动加载

该插件会自动处理Obsidian标注语法,并将其转换为Hexo可识别的格式,确保内容在博客上正确显示。