< has special meaning in markdown?

2019-03-03 00:07发布

问题:

tmp.md:

Choices for blank 91:       __A__:  pa>pb      __B__:  pa<pb      __C__: pa==pb      __D__: pa<>pb

Choices for blank 92:       __A__:  pa<>pb      __B__:  pa<pb      __C__:  pa>pb      __D__: pa==pb

Compiled with 'grip --export tmp.md' and get unexpected result:

Choices for blank 91: A: pa>pb B: papb

Choices for blank 92: A: pa<>pb B: papb D: pa==pb

It appears GRIP treated < as a meta character. But the markdown manual doesn't say so? Am I missing anything here?

回答1:

As the rules state:

For any markup that is not covered by Markdown’s syntax, you simply use HTML itself. There’s no need to preface it or delimit it to indicate that you’re switching from Markdown to HTML; you just use the tags.

Therefore, Markdown passes < and > through unaltered. However, as those characters are HTML tag deliminators, your browser will interpret anything between a < and a > as an HTML tag and it will not display it. Of course, if you never open a tag (with <), then a closing tag (>) will be ignored by the browser. Therefore, when using a < as a plain text character, it is best to use the HTML entity to ensure the browser sees it as such: &lt; (hint: Less Than => &lt; => <)

So, to use your example input:

Choices for blank 91:       __A__:  pa>pb      __B__:  pa&lt;pb      __C__: pa==pb      __D__: pa&lt;>pb

Choices for blank 92:       __A__:  pa&lt;>pb      __B__:  pa&lt;pb      __C__:  pa>pb      __D__: pa==pb

The output would look like this:

Choices for blank 91: A: pa>pb B: pa<pb C: pa==pb D: pa<>pb

Choices for blank 92: A: pa<>pb B: pa<pb C: pa>pb D: pa==pb

Note that this is the behavior of your browser. There is nothing Markdown could do to change that unless it did not allow/support using raw HTML within Markdown documents.

Of course, typing &lt; each time you want a < character is less than ideal. Generally, text which contains such characters could be classified as "code". Therefore it is usually best to just wrap it in a code span (when it is embedded in text) or a code block (when all the code consists of a block of one or more lines).

For example, this paragraph contains `<>` characters.

The above Markdown will result in the following HTML being generated:

<p>For example, this paragraph contains <code>&lt;&gt;</code> characters.</p>

Notice that the angle brackets were converted to &lt;&gt; for you (and wrapped in <code> tags). And the browser will render that as:

For example, this paragraph contains <> characters.



标签: markdown grip