moved scripts out of index.html

This commit is contained in:
Ell 2023-10-17 18:23:37 +02:00
parent f2d8da4e67
commit 1b0f3bf67a
2 changed files with 76 additions and 77 deletions

View file

@ -29,83 +29,7 @@
<body> <body>
<div class="content"> <div class="content">
<div id="main"> <div id="main">
<script> <script src="index.js"></script>
let md = markdownit({
html: true,
linkify: true,
langPrefix: "hljs language-",
highlight: (c, l) => {
const language = hljs.getLanguage(l) ? l : "plaintext";
return hljs.highlight(c, {language}).value;
}
});
md.use(texmath, {
engine: katex,
delimiters: ["dollars", "beg_end"]
});
md.use(markdownItAnchor, {
permalink: markdownItAnchor.permalink.linkInsideHeader({
placement: "after",
ariaHidden: true
}),
// prepend current file id to the anchor permalink
slugify: s => `${getId() ?? ""}-${encodeURIComponent(String(s).trim().toLowerCase().normalize("NFKD").replace(/\s+/g, "-").replace(/[^a-z0-9_-]/g, ""))}`
});
md.use(markdownitFootnote);
let rulesToReplace = [
// prepend current file id to footnotes
["footnote_ref", [/href="#(fn\d+)"/, `href="#${getId() ?? ""}-$1"`]],
["footnote_open", [/id="(fn\d+)"/, `id="${getId() ?? ""}-$1"`]],
// prepend current file id to footnote refs
["footnote_ref", [/id="(fnref\d+)"/, `id="${getId() ?? ""}-$1"`]],
["footnote_anchor", [/href="#(fnref\d+)"/, `href="#${getId() ?? ""}-$1"`]]
];
for (let replacement of rulesToReplace) {
let prevRule = md.renderer.rules[replacement[0]];
md.renderer.rules[replacement[0]] = (tokens, idx, options, env, self) => {
return prevRule(tokens, idx, options, env, self).replace(...replacement[1]);
};
}
let main = $("#main");
$(window).on("hashchange", e => {
let oldUrl = e.originalEvent?.oldURL;
let oldHash = oldUrl?.lastIndexOf("#");
if (oldHash && getId(oldUrl.substring(oldHash)) !== getId())
display();
});
display();
function display() {
main.html(`<div class="center-message"><p>Loading...</p></div>`);
let id = getId();
$.ajax({
method: "get",
url: id ? `share.php?id=${id}` : "index.md",
success: t => {
main.html(DOMPurify.sanitize(md.render(t)));
// scroll to anchor
$(() => {
let element = $(window.location.hash);
if (element.length)
$(window).scrollTop(element.offset().top);
});
},
error: (r, s, e) => main.html(`<div class="center-message"><p>Error loading shared note with id <code>${id}</code>: ${e}</p><p><a href="#">Home</a></p></div>`)
});
}
function getId(hash) {
hash ||= window.location.hash;
if (!hash)
return undefined;
// hashes consist of the file id, a dash and then the permalink anchor name
let dash = hash.indexOf("-");
return hash.substring(1, dash > 0 ? dash : hash.length);
}
</script>
</div> </div>
<div id="footer"> <div id="footer">
Created using <a href="/">Just Share Please</a> for <a href="https://obsidian.md">Obsidian</a> Created using <a href="/">Just Share Please</a> for <a href="https://obsidian.md">Obsidian</a>

75
server/public/index.js Normal file
View file

@ -0,0 +1,75 @@
let md = markdownit({
html: true,
linkify: true,
langPrefix: "hljs language-",
highlight: (c, l) => {
const language = hljs.getLanguage(l) ? l : "plaintext";
return hljs.highlight(c, {language}).value;
}
});
md.use(texmath, {
engine: katex,
delimiters: ["dollars", "beg_end"]
});
md.use(markdownItAnchor, {
permalink: markdownItAnchor.permalink.linkInsideHeader({
placement: "after",
ariaHidden: true
}),
// prepend current file id to the anchor permalink
slugify: s => `${getId() ?? ""}-${encodeURIComponent(String(s).trim().toLowerCase().normalize("NFKD").replace(/\s+/g, "-").replace(/[^a-z0-9_-]/g, ""))}`
});
md.use(markdownitFootnote);
let rulesToReplace = [
// prepend current file id to footnotes
["footnote_ref", [/href="#(fn\d+)"/, `href="#${getId() ?? ""}-$1"`]],
["footnote_open", [/id="(fn\d+)"/, `id="${getId() ?? ""}-$1"`]],
// prepend current file id to footnote refs
["footnote_ref", [/id="(fnref\d+)"/, `id="${getId() ?? ""}-$1"`]],
["footnote_anchor", [/href="#(fnref\d+)"/, `href="#${getId() ?? ""}-$1"`]]
];
for (let replacement of rulesToReplace) {
let prevRule = md.renderer.rules[replacement[0]];
md.renderer.rules[replacement[0]] = (tokens, idx, options, env, self) => {
return prevRule(tokens, idx, options, env, self).replace(...replacement[1]);
};
}
let main = $("#main");
$(window).on("hashchange", e => {
let oldUrl = e.originalEvent?.oldURL;
let oldHash = oldUrl?.lastIndexOf("#");
if (oldHash && getId(oldUrl.substring(oldHash)) !== getId())
display();
});
display();
function display() {
main.html(`<div class="center-message"><p>Loading...</p></div>`);
let id = getId();
$.ajax({
method: "get",
url: id ? `share.php?id=${id}` : "index.md",
success: t => {
main.html(DOMPurify.sanitize(md.render(t)));
// scroll to anchor
$(() => {
let element = $(window.location.hash);
if (element.length)
$(window).scrollTop(element.offset().top);
});
},
error: (r, s, e) => main.html(`<div class="center-message"><p>Error loading shared note with id <code>${id}</code>: ${e}</p><p><a href="#">Home</a></p></div>`)
});
}
function getId(hash) {
hash ||= window.location.hash;
if (!hash)
return undefined;
// hashes consist of the file id, a dash and then the permalink anchor name
let dash = hash.indexOf("-");
return hash.substring(1, dash > 0 ? dash : hash.length);
}