yoavram / markx

Markdown editor for scientific writing. Batteries included.
Other
319 stars 41 forks source link

Pandoc flavored Markdown superscript and subscript are not displayed in the html preview #41

Open ben-8409 opened 11 years ago

ben-8409 commented 11 years ago

Superscript (^letter^) and subscript (~letter~) as defined by Pandocs Markdown flavour http://johnmacfarlane.net/pandoc/README.html#superscripts-and-subscripts are not displayed in the HTML preview.

ben-8409 commented 11 years ago

Ok, i have opened an issue and the pagedown google code site (type enhancement): http://code.google.com/p/pagedown/issues/detail?id=59

I also wrote some JS ( i am a beginner in js as well as reg exp though) which seems to work for me. See my fork.

From 58a463747c1982d2bfdc17d4d9b4724e2b060859 Mon Sep 17 00:00:00 2001
From: Benjamin Geese <code@benjamingeese.de>
Date: Mon, 27 May 2013 20:45:48 +0200
Subject: [PATCH] add simple regex to display superscript and subscript in
 html preview

---
 static/js/Markdown.Converter.js |   12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/static/js/Markdown.Converter.js b/static/js/Markdown.Converter.js
index 83c068e..debd8cf 100644
--- a/static/js/Markdown.Converter.js
+++ b/static/js/Markdown.Converter.js
@@ -430,6 +430,7 @@ else

             text = _EncodeAmpsAndAngles(text);
             text = _DoItalicsAndBold(text);
+            text = _DoSubAndSuperscript(text);

             // Do hard breaks:
             text = text.replace(/  +\n/g, " <br>\n");
@@ -1060,6 +1061,17 @@ else
             return text;
         }

+        function _DoSubAndSuperscript(text) {
+
+            // sub
+            text = text.replace(/(?:~T)(?=\S)(\S*)(?:~T)/g, "<sub>$1</sub>");
+
+            // super
+            text = text.replace(/(?:\^)(?=\S)(\S*)(?:\^)/g, "<sup>$1</sup>");
+
+            return text;
+        }
+
         function _DoBlockQuotes(text) {

             /*
--
1.7.9.5

https://github.com/ben-8409/markx/branches

yoavram commented 11 years ago

Nice! The _DoSubAndSuperscript function looks good. This should be integrated via the pre or post conversion hooks in markx.js, see initMarkdownConverter at line 473. If you implement it via a hook instead of patching Markdown.Converter.js then you are very welcome to open a pull-request, I will be happy to merge it and update the online version of Markx too.

The reason I would not like to merge a change in Markdown.Converter.js is that it will not allow to update to newer pagedown versions when those become available sometime.

Cheers

karthik commented 11 years ago

Looks great. Should also be added to a toolbar at some point.

ben-8409 commented 11 years ago

Thanks for the feedback!

Nice! The |_DoSubAndSuperscript| function looks good. This should be integrated via the pre or post conversion hooks https://code.google.com/p/pagedown/wiki/PageDown in markx.js https://github.com/yoavram/markx/blob/master/static/js/markx.js, see |initMarkdownConverter| at line 473. I'll have a look at that and will implement it there later and open a pull request when ready.

The reason I would not like to merge a change in |Markdown.Converter.js| is that it will not allow to update to newer /pagedown/ versions when those become available sometime. Good point.

yoavram commented 11 years ago

@karthikram What do you mean?

karthik commented 11 years ago

@yoavram A rich formatting toolbar like this. So people can select text and click on a sup button to insert markup. Low on the list of features but would be nice.

ben-8409 commented 11 years ago

@yoavram I implementet the reg exp using the hooks as you proposed: function processDoPandocSubAndSuperscript(text) { //this adds pandoc sub and superscript support which is currently missing in pagedown text = text.replace(/(?:~)(?=\S)(\S)(?:~)/g, "$1"); text = text.replace(/(?:^)(?=\S)(\S)(?:^)/g, "$1"); return text; }

there is one issue though: pandoc allow the escaping of tags with \ . this does currently not work with my regular expressions. any ideas?

yoavram commented 11 years ago

Sorry for not responding earlier, it was a busy week. I don't understand, what is the problem?

yoavram commented 11 years ago

ping @ben-8409

ben-8409 commented 11 years ago

The problem is my basic understanding of reg expressions:

Pandoc allows you to "escape" sup and suberscript tags with a prepended "\" (backslash). This is currently not implemented in my solution above. This means ~notme~ will get subscripted but it should not according to pandoc specs. I was asking if you have any suggestions how to fix my reg expressions to support escaping?

yoavram commented 11 years ago

I see there is an open patch for this in PageDown here. The code is similar to yours, but slightly different:

function _DoSubAndSuperscript(text) {
            // sub
            text = text.replace(/(?:~T)(?=\S)(\S*)(?:~T)/g, "<sub>$1</sub>");

            // super
            text = text.replace(/(?:\^)(?=\S)(\S*)(?:\^)/g, "<sup>$1</sup>");

            return text;
}

Does this help?

yoavram commented 11 years ago

Also, this might do the trick: (?:(?!\\~)(~))(\S+)(?:(?!\\~)(~)) See it in action here

ben-8409 commented 11 years ago

I'll try both this week and inform you about me success. Would be happy to finally issue a merge request if it works.

yoavram commented 10 years ago

Hi @ben-8409 Want to pull-request your changes? Are you satisfied with them?

Y

BenjaminHoegh commented 6 years ago

@yoavram Pretty sure this does the same and it fixes an issue where it selects incorrect matches (~(?!~)([^~ ]*)~(?!~))

DEMO

yoavram commented 6 years ago

I will be happy to accept a pull request if you want to open one