-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.html
More file actions
215 lines (188 loc) · 7.53 KB
/
markdown.html
File metadata and controls
215 lines (188 loc) · 7.53 KB
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
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<script src="https://s4.zstatic.net/npm/markdown-it@13.0.1/dist/markdown-it.min.js"></script>
<script src="https://s4.zstatic.net/ajax/libs/highlight.js/11.11.1/highlight.min.js" integrity="sha512-EBLzUL8XLl+va/zAsmXwS7Z2B1F9HUHkZwyS/VKwh3S7T/U0nF4BaU29EP/ZSf6zgiIxYAnKLu6bJ8dqpmX5uw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://s4.zstatic.net/ajax/libs/highlight.js/11.11.1/styles/default.min.css" integrity="sha512-hasIneQUHlh06VNBe7f6ZcHmeRTLIaQWFd43YriJ0UND19bvYRauxthDg8E4eVNPm9bRUhr5JGeqH7FRFXQu5g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- <script src="https://s4.zstatic.net/npm/markdown-it@14.1.0/dist/markdown-it.min.js"></script> -->
<script src="https://s4.zstatic.net/npm/markdown-it-anchor@9.2.0/dist/markdownItAnchor.umd.js"></script>
<script src="https://s4.zstatic.net/npm/markdown-it-toc-done-right@4.2.0/dist/markdownItTocDoneRight.umd.js"></script>
<style>
/* 示例:基于 rem 单位,按比例缩小所有标题,但仍保持差异 */
h1 { font-size: 1.8rem; } /* 原为 2rem */
h2 { font-size: 1.6rem; } /* 原为 1.8rem */
h3 { font-size: 1.4rem; } /* 原为 1.6rem */
h4 { font-size: 1.2rem; }
h5 { font-size: 1.1rem; }
h6 { font-size: 1rem; }
#toc {
border: 1px solid #ccc;
padding: 10px;
background-color: #f9f9f9;
}
#markdown-rendered {
margin-top: 20px;
}
#backToTop {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
border: none;
outline: none;
background-color: #007BFF;
opacity: 0.2;
color: white;
cursor: pointer;
padding: 15px;
border-radius: 10px;
font-size: 18px;
}
#backToTop:hover {
background-color: #0056b3;
}
.code-container {
position: relative;
margin: 1em 0;
}
.copy-btn {
position: absolute;
right: 10px;
top: 10px;
z-index: 2;
padding: 4px 8px;
background: rgba(255, 255, 255, 0.8);
border: 1px solid #ddd;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
transition: all 0.2s;
}
.copy-btn:hover {
background: #007bff;
color: white;
border-color: transparent;
}
pre {
position: relative;
padding-top: 30px !important; /* 给按钮留出空间 */
}
</style>
</head>
<body>
<div id="markdown-rendered"></div>
<script>
const urlParams = new URLSearchParams(window.location.search);
const mdLoad = urlParams.get('md');
console.log('接收到的 md 参数值为:', mdLoad);
document.title=mdLoad;
const md = window.markdownit({
html: true,
linkify: true,
typographer: true
});
md.use(window.markdownItAnchor, { permalink: true, permalinkBefore: true, permalinkSymbol: '§' })
.use(window.markdownItTocDoneRight,{containerId: 'toc'});
// 更新后的高亮渲染逻辑(修复HTML实体转义问题)
md.renderer.rules.fence = function (tokens, idx, options, env, self) {
const token = tokens[idx];
const info = token.info ? token.info.trim() : '';
const lang = window.hljs.getLanguage(info) ? info : 'plaintext';
const code = token.content;
const highlighted = window.hljs.highlight(code, { language: lang }).value;
const uid = 'code-' + Math.random().toString(36).slice(2, 11);
return `
<div class="code-container">
<button class="copy-btn" onclick="copyCode('${uid}')">复制</button>
<!-- 隐藏元素存储原始代码 -->
<pre id="${uid}" style="display:none">${escapeSafeHTML(code)}</pre>
<!-- 显示高亮后的代码 -->
<pre><code class="hljs ${lang}">${highlighted}</code></pre>
</div>
`;
};
// 安全转义函数(保留HTML实体)
function escapeSafeHTML(str) {
return str.replace(/[<>"']/g,
ch => ({
'<': '<',
'>': '>',
'"': '"',
"'": '''
}[ch] || ch));
}
// 复制函数(保留原始格式)
function copyCode(uid) {
const pre = document.getElementById(uid);
const code = pre.textContent; // 关键点:通过textContent获取原始内容
const btn = pre.previousElementSibling;
// 添加API存在性检测(核心改动)
if (navigator.clipboard?.writeText) {
navigator.clipboard.writeText(code).then(() => {
btn.textContent = '已复制!';
setTimeout(() => btn.textContent = '复制', 2000);
}).catch(() => {
// 现代API失败时仍然走旧方案
execCommandCopy(code, btn);
});
} else {
// 直接使用旧方案
execCommandCopy(code, btn);
}
// 保持原有逻辑的最小封装(仅提取重复代码)
function execCommandCopy(code, btn) {
const textarea = document.createElement('textarea');
textarea.value = code;
// 修复问题(2)(3):添加隐藏和防键盘逻辑
textarea.style.position = 'fixed';
textarea.style.opacity = 0;
textarea.setAttribute('readonly', ''); // 修复iOS问题
document.body.appendChild(textarea);
// 强化选择逻辑
textarea.select();
if (textarea.setSelectionRange) { // 修复选择问题
textarea.setSelectionRange(0, textarea.value.length);
}
try {
document.execCommand('copy');
btn.textContent = '已复制!';
setTimeout(() => btn.textContent = '复制', 2000);
} finally {
document.body.removeChild(textarea);
}
}
}
const externalMdUrl = './md/'+mdLoad+'.md';
fetch(externalMdUrl)
.then(response => response.text())
.then(data => {
const html=md.render('${toc}\n'+data);
document.getElementById('markdown-rendered').innerHTML = html+"<br><br>";
})
.catch(error => {
console.error('Error fetching Markdown file:', error);
});
</script>
<button id="backToTop" onclick="scrollToTop()">顶</button>
<script>
function scrollToTop() {
window.scrollTo({
top: 0,
behavior:'smooth'
});
}
window.onscroll = function () {
const backToTopButton = document.getElementById('backToTop');
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
backToTopButton.style.display = 'block';
} else {
backToTopButton.style.display = 'none';
}
};
</script>
</body>
</html>