How would you feel about automatic detection of Textile and Markdown?

I am considering supporting Textile and Markdown in the current project. I would prefer not to force users to choose one or the other. Is there a way to automatically determine what the user is using? How would you do that? I would like to find / develop both JavaScript and a PHP solution so that I can provide real-time previews as well as handle user input on the server side.

+4
source share
3 answers

Note that users can use only one syntax element in the wiring, so you will need to check everything. We are looking for "h1". obviously only works if the user uses this particular element.

It's pretty easy with things like headers, but keep in mind that *this* markup formats are like <em>this</em> , and Textile converts that value to <strong>this</strong> . Thus, you will have ambiguous syntax constructs that will bring different results in each language.

I would suggest going with a user choice. Try to find out which syntax your users (or you) usually prefer, check the box "use x instead of y" for those who want a different choice.

+6
source

It really shouldn't be that hard. Markdown does not support the following syntax:

 h1. Header p. Paragraph 

... so you just scan it to see if it is textile. A very simple regular expression to get you started (scanning lines starting with hX or p.) In the PHP code:

 if (preg_match('/^(p|h[1-6])\. /m', $subject)) { // Successful match } else { // Match attempt failed } 

You will probably be able to write your own regular expression for the Markdown scan.

+3
source

Automatic detection, I do not know, are both based on "natural" typing.
Perhaps you can ask the user to select a format using a pair of radio buttons or something like that.

+1
source

Source: https://habr.com/ru/post/1277204/


All Articles