Can I define a class name on paragraph using Markdown?

后端 未结 10 996
灰色年华
灰色年华 2020-12-02 08:04

Can I define a class name on paragraph using Markdown? If so, how?

相关标签:
10条回答
  • 2020-12-02 08:32

    Here is a working example for kramdown following @Yarin's answer.

    A simple paragraph with a class attribute.
    {:.yourClass}
    

    Reference: https://kramdown.gettalong.org/syntax.html#inline-attribute-lists

    0 讨论(0)
  • 2020-12-02 08:32

    It should also be mentioned that <span> tags allow inside them -- block-level items negate MD natively inside them unless you configure them not to do so, but in-line styles natively allow MD within them. As such, I often do something akin to...

    This is a superfluous paragraph thing.
    
    <span class="class-red">And thus I delve into my topic, Lorem ipsum lollipop bubblegum.</span>
    
    And thus with that I conclude.
    

    I am not 100% sure if this is universal but seems to be the case in all MD editors I've used.

    0 讨论(0)
  • 2020-12-02 08:39

    If your environment is JavaScript, use markdown-it along with the plugin markdown-it-attrs:

    const md = require('markdown-it')();
    const attrs = require('markdown-it-attrs');
    md.use(attrs);
    
    const src = 'paragraph {.className #id and=attributes}';
    
    // render
    let res = md.render(src);
    console.log(res);
    

    Output

    <p class="className" id="id" and="attributes">paragraph</p>
    

    jsfiddle

    Note: Be aware of the security aspect when allowing attributes in your markdown!

    Disclaimer, I'm the author of markdown-it-attrs.

    0 讨论(0)
  • 2020-12-02 08:41

    Markdown should have this capability, but it doesn't. Instead, you're stuck with language-specific Markdown supersets:

    PHP: Markdown Extra
    Ruby: Kramdown, Maruku

    But if you need to abide by true Markdown syntax, you're stuck with inserting raw HTML, which is less ideal.

    0 讨论(0)
  • 2020-12-02 08:42

    Dupe: How do I set an HTML class attribute in Markdown?


    Natively? No. But...

    No, Markdown's syntax can't. You can set ID values with Markdown Extra through.

    You can use regular HTML if you like, and add the attribute markdown="1" to continue markdown-conversion within the HTML element. This requires Markdown Extra though.

    <p class='specialParagraph' markdown='1'>
    **Another paragraph** which allows *Markdown* within it.
    </p>
    

    Possible Solution: (Untested and intended for <blockquote>)

    I found the following online:

    Function

    function _DoBlockQuotes_callback($matches) {
    
        ...cut...
    
        //add id and class details...
        $id = $class = '';
        if(preg_match_all('/\{(?:([#.][-_:a-zA-Z0-9 ]+)+)\}/',$bq,$matches)) {
            foreach ($matches[1] as $match) {
                if($match[0]=='#') $type = 'id';
                else $type = 'class';
                ${$type} = ' '.$type.'="'.trim($match,'.# ').'"';
            }
            foreach ($matches[0] as $match) {
                $bq = str_replace($match,'',$bq);
            }
        }
    
        return _HashBlock(
            "<blockquote{$id}{$class}>\n$bq\n</blockquote>"
        ) . "\n\n";
    }
    

    Markdown

    >{.className}{#id}This is the blockquote
    

    Result

    <blockquote id="id" class="className">
        <p>This is the blockquote</p>
    </blockquote>
    
    0 讨论(0)
  • 2020-12-02 08:49

    In slim markdown use this:

    markdown:
      {:.cool-heading}
      #Some Title
    

    Translates to:

    <h1 class="cool-heading">Some Title</h1>
    
    0 讨论(0)
提交回复
热议问题