tablaster.dev/markdown.js

25 lines
No EOL
710 B
JavaScript

function loadMarkdownFile(fileName) {
fetch(`/assets/md/${fileName}`)
.then(response => response.text())
.then(text => {
document.getElementById('markdown').innerHTML = convertMarkdownToHTML(text);
addBlankTargetToLinks();
})
.catch(error => {
console.error('Error loading Markdown file:', error);
});
}
function convertMarkdownToHTML(markdown) {
return marked.parse(markdown);
}
function addBlankTargetToLinks() {
const links = document.querySelectorAll('a');
links.forEach(link => {
if (!link.getAttribute('href').startsWith('http') && !link.getAttribute('href').startsWith('https')) {
return;
}
link.setAttribute('target', '_blank');
});
}