From 681dc8fbc16affd309982e6be940153f43ab11eb Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Tue, 3 Oct 2023 16:09:11 +0530 Subject: [PATCH] fix: pdf rendering in lessons --- lms/hooks.py | 1 + lms/plugins.py | 4 ++++ lms/www/batch/edit.js | 44 ++++++++++++++++++++++++++----------------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/lms/hooks.py b/lms/hooks.py index a8913578..0c82242b 100644 --- a/lms/hooks.py +++ b/lms/hooks.py @@ -306,6 +306,7 @@ lms_markdown_macro_renderers = { "Assignment": "lms.plugins.assignment_renderer", "Embed": "lms.plugins.embed_renderer", "Audio": "lms.plugins.audio_renderer", + "PDF": "lms.plugins.pdf_renderer", } # page_renderer to manage profile pages diff --git a/lms/plugins.py b/lms/plugins.py index 590820f3..b0f40cd2 100644 --- a/lms/plugins.py +++ b/lms/plugins.py @@ -185,6 +185,10 @@ def audio_renderer(src): return f"" +def pdf_renderer(src): + return f"" + + def assignment_renderer(detail): supported_types = { "Document": ".doc,.docx,.xml,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document", diff --git a/lms/www/batch/edit.js b/lms/www/batch/edit.js index 8922626b..65318ccc 100644 --- a/lms/www/batch/edit.js +++ b/lms/www/batch/edit.js @@ -54,11 +54,6 @@ const get_tools = () => { "https://docs.google.com/presentation/d/e/<%= remote_id %>/embed", html: "", }, - pdf: { - regex: /(https?:\/\/.*\.pdf)/, - embedUrl: "<%= remote_id %>", - html: "", - }, }, }, }, @@ -131,6 +126,15 @@ const parse_string_to_lesson = (type) => { file_type: "audio", }, }); + } else if (block.includes("{{ PDF")) { + let pdf = block.match(/\(["']([^"']+?)["']\)/)[1]; + blocks.push({ + type: "upload", + data: { + file_url: pdf, + file_type: "pdf", + }, + }); } else if (block.includes("{{ Embed")) { let embed = block.match(/\(["']([^"']+?)["']\)/)[1]; blocks.push({ @@ -195,23 +199,21 @@ const parse_content_to_string = (data, type) => { lesson_content += `{{ Quiz("${block.data.quiz}") }}\n`; } else if (block.type == "upload") { let url = block.data.file_url; - lesson_content += - block.data.file_type == "video" - ? `{{ Video("${url}") }}\n` - : block.data.file_type == "audio" - ? `{{ Audio("${url}") }}\n` - : `![](${url})`; + if (block.data.file_type == "video") { + lesson_content += `{{ Video("${url}") }}\n`; + } else if (block.data.file_type == "audio") { + lesson_content += `{{ Audio("${url}") }}\n`; + } else if (block.data.file_type == "pdf") { + lesson_content += `{{ PDF("${url}") }}\n`; + } else { + lesson_content += `![](${url})`; + } } else if (block.type == "header") { lesson_content += "#".repeat(block.data.level) + ` ${block.data.text}\n`; } else if (block.type == "paragraph") { lesson_content += `${block.data.text}\n`; } else if (block.type == "embed") { - if (block.data.service == "pdf") { - if (!block.data.embed.startsWith(window.location.origin)) { - frappe.throw(__("Invalid PDF URL")); - } - } lesson_content += `{{ Embed("${ block.data.service }|||${block.data.embed.replace(/&/g, "&")}") }}\n`; @@ -287,6 +289,10 @@ const get_file_type = (url) => { return "audio"; } + if (url.split(".").pop() == "pdf") { + return "pdf"; + } + return "image"; }; @@ -468,7 +474,7 @@ class Upload { folder: "Home/Attachments", make_attachments_public: true, restrictions: { - allowed_file_types: ["image/*", "video/*", "audio/*"], + allowed_file_types: ["image/*", "video/*", "audio/*", ".pdf"], }, on_success: (file_doc) => { self.file_url = file_doc.file_url; @@ -487,6 +493,10 @@ class Upload { return ``; + } else if (this.file_type == "pdf") { + return ``; } else { return ``; }