This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/src/parseMarkdown.js

28 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-09-07 11:01:52 -07:00
export default async function parseMarkdown(markdown) {
try {
const content = '<div>' +
markdown
.replace(/<table>/g, '<div class="table-responsive"><table class="table table-striped small">')
.replace(/<\/table>/g, '</table></div>')
2021-11-01 11:27:58 -07:00
.replace(/{favicon=(.+?)}/g, '<img src="https://$1" alt="Favicon" style="width: 1em; height: 1em;"/>')
2021-09-07 11:01:52 -07:00
.replace(/<a href="http/g, '<a target="_blank" rel="noopener" href="http')
2021-11-01 13:43:45 -07:00
.replace(/<p>{details=(.+?)}<\/p>(.+?)<p>{\/details}<\/p>/gms, '<details class="border mb-3"><summary class="bg-light p-3">$1</summary><div class="border-top p-3 bg-white">$2</div></details>')
2021-09-07 11:01:52 -07:00
+ '</div>'
;
const titleMatch = content.match('<h1[^>]*>([^<]+)</h1>');
const title = titleMatch ? titleMatch[1] : null;
const imgMatch = content.match('<img src="([^"]+)"[^>]*>');
const img = imgMatch ? imgMatch[1] : null;
return {
content,
title,
img,
}
} catch {
return {
content: null,
};
}
}