diff --git a/nyaa/forms.py b/nyaa/forms.py index ba0ad60..334031f 100644 --- a/nyaa/forms.py +++ b/nyaa/forms.py @@ -168,7 +168,7 @@ class EditForm(FlaskForm): information = StringField('Information', [ Length(max=255, message='Information must be at most %(max)d characters long.') ]) - description = TextAreaField('Description (markdown supported)', [ + description = TextAreaField('Description', [ Length(max=10 * 1024, message='Description must be at most %(max)d characters long.') ]) @@ -222,7 +222,7 @@ class UploadForm(FlaskForm): information = StringField('Information', [ Length(max=255, message='Information must be at most %(max)d characters long.') ]) - description = TextAreaField('Description (markdown supported)', [ + description = TextAreaField('Description', [ Length(max=10 * 1024, message='Description must be at most %(max)d characters long.') ]) diff --git a/nyaa/static/js/main.js b/nyaa/static/js/main.js index 9fd1a5d..7f6c48e 100644 --- a/nyaa/static/js/main.js +++ b/nyaa/static/js/main.js @@ -145,28 +145,46 @@ document.addEventListener("DOMContentLoaded", function(event) { } }); +var markdownOptions = { + html : false, + breaks : true, + typographer: true +} +var markdown = window.markdownit(markdownOptions); +markdown.renderer.rules.table_open = function (tokens, idx) { + // Format tables nicer (bootstrap). Force auto-width (default is 100%) + return ''; +} + // Initialise markdown editors on page document.addEventListener("DOMContentLoaded", function() { - var markdownEditors = Array.prototype.slice.call(document.querySelectorAll('.markdown-editor')); + var markdownEditors = Array.prototype.slice.call(document.querySelectorAll('.markdown-editor')); - markdownEditors.forEach(function (markdownEditor) { - var fieldName = markdownEditor.getAttribute('data-field-name'); + markdownEditors.forEach(function (markdownEditor) { + var fieldName = markdownEditor.getAttribute('data-field-name'); - var previewTabSelector = '#' + fieldName + '-preview-tab'; - var targetSelector = '#' + fieldName + '-markdown-target'; - var sourceSelector = markdownEditor.querySelector('.markdown-source'); + var previewTabSelector = '#' + fieldName + '-preview-tab'; + var targetSelector = '#' + fieldName + '-markdown-target'; + var sourceSelector = markdownEditor.querySelector('.markdown-source'); - var previewTabEl = markdownEditor.querySelector(previewTabSelector); - var targetEl = markdownEditor.querySelector(targetSelector); + var previewTabEl = markdownEditor.querySelector(previewTabSelector); + var targetEl = markdownEditor.querySelector(targetSelector); - var reader = new commonmark.Parser({safe: true}); - var writer = new commonmark.HtmlRenderer({safe: true, softbreak: '
'}); + previewTabEl.addEventListener('click', function () { + var rendered = markdown.render(sourceSelector.value.trim()); + targetEl.innerHTML = rendered; + }); + }); +}); - previewTabEl.addEventListener('click', function () { - var parsed = reader.parse(sourceSelector.value.trim()); - targetEl.innerHTML = writer.render(parsed); - }); - }); +// Render markdown from elements with "markdown-text" attribute +document.addEventListener("DOMContentLoaded", function() { + + var markdownTargets = document.querySelectorAll('[markdown-text]'); + for (var target of markdownTargets) { + var rendered = markdown.render(target.innerHTML); + target.innerHTML = rendered; + } }); // diff --git a/nyaa/templates/_formhelpers.html b/nyaa/templates/_formhelpers.html index 02fb743..212c753 100644 --- a/nyaa/templates/_formhelpers.html +++ b/nyaa/templates/_formhelpers.html @@ -35,6 +35,7 @@ {% endif %}
{{ field.label(class='control-label') }} + Markdown supported
-
- {% if torrent.description %} +
+ {%- if torrent.description -%} {# Escape newlines into html entities because CF strips blank newlines #} - {{ torrent.description | escape | replace('\r\n', '\n') | replace('\n', ' '|safe) }} - {% else %} + {{- torrent.description | escape | replace('\r\n', '\n') | replace('\n', ' '|safe) -}} + {%- else -%} #### No description. - {% endif%} + {%- endif -%}
@@ -161,19 +161,12 @@
{# Escape newlines into html entities because CF strips blank newlines #} -
{{ comment.text }}
+
{{ comment.text }}
- + {% endfor %} {% if comment_form %}
@@ -207,13 +200,4 @@ - - {% endblock %} \ No newline at end of file