resume/docker/resume/markdown-loader.js

63 lines
1.7 KiB
JavaScript

// Load same-named .md and render into .story-content using Marked. No fallbacks.
(function () {
function getMarkdownPath() {
var parts = (window.location.pathname || '').split('/');
var last = parts[parts.length - 1] || '';
if (!last) return '';
return last.replace(/\.html?$/i, '.md');
}
function loadMarked(callback) {
if (window.marked && typeof window.marked.parse === 'function') {
callback();
return;
}
var script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/marked/marked.min.js';
script.async = true;
script.onload = callback;
document.head.appendChild(script);
}
function renderMarkdown(container, text) {
if (!window.marked || typeof window.marked.parse !== 'function') {
container.innerHTML = '';
return;
}
try {
if (typeof window.marked.setOptions === 'function') {
window.marked.setOptions({ gfm: true, breaks: true });
}
container.innerHTML = window.marked.parse(text);
} catch (_) {
container.innerHTML = '';
}
}
function init() {
var container = document.querySelector('.story-content');
if (!container) return;
// No fallback: clear immediately
container.innerHTML = '';
var mdPath = getMarkdownPath();
if (!mdPath) return;
loadMarked(function () {
fetch(mdPath, { cache: 'no-cache' })
.then(function (res) { if (!res.ok) throw new Error('md'); return res.text(); })
.then(function (text) { renderMarkdown(container, text); })
.catch(function () { container.innerHTML = ''; });
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();