diff --git a/assets/controllers/common/markdown_controller.js b/assets/controllers/common/markdown_controller.js new file mode 100644 index 00000000..cba78b49 --- /dev/null +++ b/assets/controllers/common/markdown_controller.js @@ -0,0 +1,56 @@ +'use strict'; + +import { Controller } from '@hotwired/stimulus'; +import * as marked from "marked"; +import DOMPurify from 'dompurify'; + +export default class extends Controller { + + connect() + { + console.log('Markdown controller called'); + this.render(); + } + + render() { + let raw = this.element.dataset['markdown']; + + //Apply purified parsed markdown + this.element.innerHTML = DOMPurify.sanitize(marked(this.unescapeHTML(raw))); + + for(let a of this.element.querySelectorAll('a')) { + //Mark all links as external + a.classList.add('link-external'); + //Open links in new tag + a.setAttribute('target', '_blank'); + //Dont track + a.setAttribute('rel', 'noopener'); + } + + //Apply bootstrap styles to + for(let table of this.element.querySelectorAll('table')) { + table.classList.add('table', 'table-hover', 'table-striped', 'table-bordered', 'table-sm'); + } + } + + /** + * Unescape the given HTML + * @param {string} html + * @returns {string} + */ + unescapeHTML(html) { + var txt = document.createElement('textarea'); + txt.innerHTML = html; + return txt.value; + } + + /** + * Configure the marked parser + */ + configureMarked() + { + marked.setOptions({ + gfm: true, + }); + } +} \ No newline at end of file diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index 5d3bdc81..8f9d1586 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -15,7 +15,8 @@ doctrine: class: App\Helpers\BigDecimalType schema_filter: ~^(?!internal)~ - profiling_collect_backtrace: true + # Only enable this when needed + profiling_collect_backtrace: false orm: auto_generate_proxy_classes: true diff --git a/src/Services/MarkdownParser.php b/src/Services/MarkdownParser.php index 224a15a5..fc25b1ad 100644 --- a/src/Services/MarkdownParser.php +++ b/src/Services/MarkdownParser.php @@ -68,7 +68,7 @@ class MarkdownParser public function markForRendering(string $markdown, bool $inline_mode = false): string { return sprintf( - '