aboutsummaryrefslogtreecommitdiff
path: root/static/assets/js
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-03-21 01:28:56 -0400
committerBobby <[email protected]>2022-03-21 01:28:56 -0400
commit6a43cd05abf6be7d30e405f2e797738ffeaf659a (patch)
tree1d89ba30395fc82e7b41a30610007363af79de26 /static/assets/js
parentb38eaa1a814769d93b7cd8348804fc1241098189 (diff)
downloadluciferreeves.github.io-6a43cd05abf6be7d30e405f2e797738ffeaf659a.tar.xz
luciferreeves.github.io-6a43cd05abf6be7d30e405f2e797738ffeaf659a.zip
update posts function
Diffstat (limited to 'static/assets/js')
-rw-r--r--static/assets/js/pages/publish.js75
1 files changed, 75 insertions, 0 deletions
diff --git a/static/assets/js/pages/publish.js b/static/assets/js/pages/publish.js
new file mode 100644
index 0000000..7349fac
--- /dev/null
+++ b/static/assets/js/pages/publish.js
@@ -0,0 +1,75 @@
+window.addEventListener("DOMContentLoaded", () => {
+ const content = document.getElementById("content");
+ const renderPreview = document.getElementById("renderPreview");
+ marked.setOptions({
+ highlight: function (code) {
+ return hljs.highlightAuto(code).value;
+ },
+ });
+ content.addEventListener("input", () => {
+ renderPreview.innerHTML = marked.parse(content.value);
+ renderMathInElement(renderPreview, {
+ // customised options
+ // • auto-render specific keys, e.g.:
+ delimiters: [
+ { left: "$$", right: "$$", display: true },
+ { left: "$", right: "$", display: false },
+ { left: "\\(", right: "\\)", display: false },
+ { left: "\\[", right: "\\]", display: true },
+ ],
+ // • rendering keys, e.g.:
+ throwOnError: false,
+ });
+ });
+});
+$(document).on("click", "#publishPost", () => {
+ document.getElementById("error").classList.add("hidden");
+ const content = $("#content").val();
+ const title = $("#title").val();
+ const publishDate = $("#publishDate").val();
+ const slug = title
+ .toLowerCase()
+ .replace(/ /g, "-")
+ .replace(/[^\w-]+/g, "");
+ const tags = $("#tags").val();
+ if (title === "" || publishDate === "") {
+ document.getElementById("error").classList.remove("hidden");
+ return;
+ } else {
+ // Publish post to api/blog/new
+ const body = {
+ title: title,
+ publishDate: publishDate,
+ tags: tags,
+ content: content,
+ shortText: marked.parse(content.substring(0, 120) + "..."),
+ slug: slug,
+ };
+ if (window.location.href.includes(slug)) {
+ // Update post
+ $.ajax({
+ url: `/api/blog/update/${slug}`,
+ type: "PUT",
+ data: body,
+ success: function (data) {
+ window.location.href = `/posts/${slug}`;
+ },
+ error: function (err) {
+ console.log(err);
+ },
+ });
+ } else {
+ $.ajax({
+ url: "/api/blog/new",
+ type: "POST",
+ data: body,
+ success: (data) => {
+ window.location.href = "/admin/dashboard";
+ },
+ error: (err) => {
+ console.log(err);
+ },
+ });
+ }
+ }
+});