Skip to content

Commit 75eaff7

Browse files
committed
feat: support query params
1 parent 79845c2 commit 75eaff7

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

pages/mathjax-md-preview/index.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,45 @@ <h1>Markdown Preview <small>with MathJax</small></h1>
524524
console.log("Setting up initial preview...");
525525
// Use a small delay to ensure MathJax has a chance to load, especially on slower connections
526526
// Although the typesetPromise logic should handle readiness checks.
527+
// TODO: 这里不太对,有的时候好像会报错,我们应该找一个可以订阅是否 init 结束的地方
527528
setTimeout(updatePreview, 150);
528529

530+
/**
531+
* 读取某个 query 参数 用于附加功能
532+
* @param {string} key - 参数名称
533+
* @param {function} callbackFn - 回调函数
534+
*/
535+
function readQueryParam(key, callbackFn) {
536+
const urlParams = new URLSearchParams(window.location.search);
537+
const value = urlParams.get(key);
538+
if (value !== null) {
539+
callbackFn(value);
540+
}
541+
}
542+
543+
// 附加功能: 从 URL 参数中加载 Markdown Url
544+
readQueryParam('mdUrl', (markdownUrl) => {
545+
fetch(markdownUrl)
546+
.then(response => response.text())
547+
.then(markdownText => {
548+
markdownInput.value = markdownText;
549+
debouncedUpdatePreview();
550+
})
551+
.catch(error => {
552+
console.error("Error fetching Markdown:", error);
553+
alert("Error fetching Markdown: " + error);
554+
});
555+
});
556+
// 附加功能: 从 URL 参数中加载 Markdown Text (base64编码)
557+
readQueryParam('mdText', (markdownText) => {
558+
try {
559+
markdownInput.value = decodeURIComponent(atob(markdownText));
560+
} catch (error) {
561+
console.error("Error decoding Markdown:", error);
562+
alert("Error decoding Markdown: " + error);
563+
}
564+
debouncedUpdatePreview();
565+
});
529566
}); // End of DOMContentLoaded listener
530567
</script>
531568

0 commit comments

Comments
 (0)