2025-05-27 15:46:31 +08:00

34 lines
1.1 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function loadMarkdown(md_file, htmlContainer) {
// 创建一个自定义渲染器
const renderer = new marked.Renderer();
// 自定义图片的渲染方法
renderer.image = function (obj) {
// 将 href、title 和 text 传入自定义 HTML加入自定义属性和样式
const encodedHref = encodeURI(obj.href);
return `
<div class="custom-image-container">
<img src="${obj.href}" alt="${obj.text}" class="custom-image">
</div>
`;
};
marked.setOptions({
renderer: renderer,
// 可选的其他 marked 配置
});
let fetchUrl = `/static/docs/${md_file}.md`;
let htmlContainerID = htmlContainer || "markdown-section";
fetch(fetchUrl) // 请求你的本地 md 文件
// fetch('{{ url_for("static", filename="docs/使用说明.md") }}') // 请求你的本地 md 文件
.then((response) => response.text())
.then((mdContent) => {
const htmlContent = marked.parse(mdContent); // 使用 marked.js 转换为 HTML
document.getElementById(htmlContainerID).innerHTML = htmlContent; // 渲染到页面
})
.catch((error) => console.error("Error loading markdown file:", error));
}