markx icon indicating copy to clipboard operation
markx copied to clipboard

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

Open ben-8409 opened this issue 12 years ago • 16 comments

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 avatar May 27 '13 15:05 ben-8409

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 <[email protected]>
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

ben-8409 avatar May 27 '13 19:05 ben-8409

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

yoavram avatar May 28 '13 08:05 yoavram

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

karthik avatar May 28 '13 19:05 karthik

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.

ben-8409 avatar May 29 '13 06:05 ben-8409

@karthikram What do you mean?

yoavram avatar May 30 '13 12:05 yoavram

@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.

karthik avatar May 30 '13 21:05 karthik

@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?

ben-8409 avatar Jun 13 '13 09:06 ben-8409

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

yoavram avatar Jun 21 '13 06:06 yoavram

ping @ben-8409

yoavram avatar Jul 09 '13 11:07 yoavram

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?

ben-8409 avatar Jul 12 '13 14:07 ben-8409

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 avatar Jul 22 '13 08:07 yoavram

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

yoavram avatar Jul 22 '13 09:07 yoavram

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

ben-8409 avatar Jul 22 '13 09:07 ben-8409

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

Y

yoavram avatar Mar 12 '14 10:03 yoavram

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

DEMO

BenjaminHoegh avatar Jun 08 '18 21:06 BenjaminHoegh

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

yoavram avatar Jun 09 '18 11:06 yoavram