Camen Design Forum

Kroc, why did you bestow upon me this nightmare?

Kroc

Something that immediately strikes me is that since JS doesn't support recursive regexes, the PRE block is not going to work. You should be feeding your script documentation.rem as a more robust test. Also, don't change the placeholder style! You appear to have added : and - to it, this won't work; the placeholder is the way it is because it is always the exact size of the content it is replacing, which is necessary for wordwrapping. By adding extra characters to it, you will break the wordwrapping, as well as that ReMarkable will crash with the "placeholders already in the markup" test and the title-casing code.

ReMarkable was written to compliment PHPs functionality and would be an entirely different beast if I were to write it in JS. Much of the complexity in the regexes (especially with look-behinds) is to do with the basic principle of how ReMarkable splits the content into chunks. In essence, what ReMarkable does is ensure that every paragraph has a blank line before and after it and any block-level elements (such as OL / DL) are on a separate line, not directly on the paragraph text. This allows something like this to have paragraphs added quickly and reliably:

~~~
<ol>
<li>

The quick brown fox.

Jumps over the lazy dog.

</li>
</ol>
~~~

The regexes have to do various checks to find the blank lines where each chunk starts and stops and must also check where lists come to an end and some other content follows. The lists are very complicated because ReMarkable has to assess whether the line before and after a list item is the beginning or end of the whole list or just an optional blank line between list items, and whether this blank line belongs to one list item or another one. It was a real headache to write and took something like a week to write that regex alone.

Because these regexes are operating on the whole source text, they have to be careful not to step on other content. This is where the JavaScript regex limitations are going to suffer badly.

If I were doing ReMarkable from scratch in JS (where closures are cheap), I would instead chunk the content in to multi-level object arrays, based on type.

For example, if you use regex to recognise a whole list, you would remove the whole list from the source text and put it into an array of removed chunks with everything else, and then use a sub-array for each of the list items (so that the list-item regex would not have to be aware of what was above / below the list). With all the source text broken into a nested array that represents actual depth, doing the word-wrapping, output and indenting would be very cheap.

This is also how I would probably do it when -- if ever -- my host upgrades to PHP5.3, where I can use cheap closures.

As it stands I cannot see anyway that JS can recursively recognise the PRE fences and this syntax will have to be changed to better support weak regex engines.

Append

(Leave this as-is, it’s a trap!)

Only the original author or a moderator can append to this post.

Pro tip: Use markup to add links, quotes and more.

Your friendly neighbourhood moderators: Kroc, Impressed, Martijn