markerdown-为memos增加快捷输入md语法的注入项
只需要简单的js注入即可实现输入框快捷输入md语法
一 预览
效果预览如下,兼容多种主题风格,方便在输入文字的过程中直接使用md语法:


二 实现方式
将以下代码粘贴进memos中的自定义脚本中即可
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
(function() {
// 1. 工具配置
const tools = [
{ label: "标题 1", prefix: "# ", suffix: "", icon: "H1" },
{ label: "标题 2", prefix: "## ", suffix: "", icon: "H2" },
{ label: "标题 3", prefix: "### ", suffix: "", icon: "H3" },
{ label: "加粗", prefix: "**", suffix: "**", icon: "<b>B</b>" },
{ label: "斜体", prefix: "*", suffix: "*", icon: "<i>I</i>" },
{ label: "删除线", prefix: "~~", suffix: "~~", icon: "<s>S</s>" },
{ label: "高亮", prefix: "==", suffix: "==", icon: "M" },
{ label: "代码块", prefix: "```\n", suffix: "\n```", icon: "{}" },
{ label: "引用", prefix: "> ", suffix: "", icon: "“" },
{ label: "任务", prefix: "- [ ] ", suffix: "", icon: "☑" },
{ label: "列表", prefix: "- ", suffix: "", icon: "•" },
{ label: "表格", prefix: "| 标题 | 标题 |\n| --- | --- |\n| 内容 | 内容 |", suffix: "", icon: "田" },
{ label: "链接", prefix: "[", suffix: "](url)", icon: "🔗" },
{ label: "图片", prefix: "", icon: "🖼" },
{ label: "公式", prefix: "$", suffix: "$", icon: "∑" },
{ label: "分割线", prefix: "\n---\n", suffix: "", icon: "—" },
];
// 2. 注入兼容 Memos 主题的 CSS
const styleId = 'memos-markdown-toolbar-style';
if (!document.getElementById(styleId)) {
const style = document.createElement('style');
style.id = styleId;
style.innerHTML = `
.memos-md-toolbar {
display: flex;
flex-wrap: wrap;
gap: 2px;
padding: 4px;
/* 直接使用 Memos 内部变量 */
background-color: var(--zinc-100);
border: 1px solid var(--zinc-200);
border-bottom: none;
border-radius: 0.5rem 0.5rem 0 0;
width: 100%;
box-sizing: border-box;
}
/* 暗色模式适配 */
.dark .memos-md-toolbar {
background-color: var(--zinc-800);
border-color: var(--zinc-700);
}
.md-toolbar-btn {
display: flex;
align-items: center;
justify-content: center;
min-width: 30px;
height: 28px;
font-size: 13px;
border-radius: 4px;
border: none;
background: transparent;
cursor: pointer;
/* 继承 Memos 的文字颜色 */
color: var(--text-zinc-500);
transition: all 0.2s;
}
.md-toolbar-btn:hover {
background-color: var(--zinc-200);
color: var(--text-zinc-700);
}
.dark .md-toolbar-btn:hover {
background-color: var(--zinc-700);
color: var(--text-zinc-300);
}
/* 修正输入框样式以衔接 */
.memos-editor-textarea {
border-top-left-radius: 0 !important;
border-top-right-radius: 0 !important;
}
`;
document.head.appendChild(style);
}
// 3. 插入逻辑
function insertText(textarea, prefix, suffix) {
if (!textarea) return;
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const text = textarea.value;
const selection = text.substring(start, end);
const newVal = text.substring(0, start) + prefix + selection + suffix + text.substring(end);
textarea.value = newVal;
textarea.dispatchEvent(new Event('input', { bubbles: true }));
textarea.focus();
const cursorPos = start + prefix.length + selection.length;
textarea.setSelectionRange(cursorPos, cursorPos);
}
function createToolbar(textarea) {
if (textarea.getAttribute('data-has-md-toolbar')) return;
const parent = textarea.parentElement;
if (!parent) return;
textarea.setAttribute('data-has-md-toolbar', 'true');
textarea.classList.add('memos-editor-textarea');
const toolbar = document.createElement('div');
toolbar.className = 'memos-md-toolbar';
tools.forEach(tool => {
const btn = document.createElement('button');
btn.className = 'md-toolbar-btn';
btn.innerHTML = tool.icon;
btn.title = tool.label;
btn.type = 'button';
btn.onclick = (e) => {
e.preventDefault();
e.stopPropagation();
insertText(textarea, tool.prefix, tool.suffix);
};
toolbar.appendChild(btn);
});
parent.style.flexDirection = 'column';
parent.insertBefore(toolbar, textarea);
}
function scan() {
// 兼容首页、弹窗以及不同语言下的 textarea
const textareas = document.querySelectorAll('textarea');
textareas.forEach(el => {
if (el.placeholder.includes('记') ||
el.placeholder.includes('What') ||
el.classList.contains('resize-none')) {
createToolbar(el);
}
});
}
// 监听 DOM 变化
const observer = new MutationObserver(scan);
observer.observe(document.body, { childList: true, subtree: true });
// 初始化和周期性扫描
setTimeout(scan, 500);
setInterval(scan, 2000);
console.log("Memos v0.25+ Theme-Adaptive Toolbar Loaded.");
})();