Can I define a class name on paragraph using Markdown?

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

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

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

    Raw HTML is actually perfectly valid in markdown. For instance:

    Normal *markdown* paragraph.
    
    <p class="myclass">This paragraph has a class "myclass"</p>
    

    Just make sure the HTML is not inside a code block.

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

    If your flavour of markdown is kramdown, then you can set css class like this:

    {:.nameofclass}
    paragraph is here
    

    Then in you css file, you set the css like this:

    .nameofclass{
       color: #000;
      }
    
    0 讨论(0)
  • 2020-12-02 08:56

    As mentioned above markdown itself leaves you hanging on this. However, depending on the implementation there are some workarounds:

    At least one version of MD considers <div> to be a block level tag but <DIV> is just text. All broswers however are case insensitive. This allows you to keep the syntax simplicity of MD, at the cost of adding div container tags.

    So the following is a workaround:

    <DIV class=foo>
    
      Paragraphs here inherit class foo from above.
    
    </div>
    

    The downside of this is that the output code has <p> tags wrapping the <div> lines (both of them, the first because it's not and the second because it doesn't match. No browser fusses about this that I've found, but the code won't validate. MD tends to put in spare <p> tags anyway.

    Several versions of markdown implement the convention <tag markdown="1"> in which case MD will do the normal processing inside the tag. The above example becomes:

    <div markdown="1" class=foo>
    
      Paragraphs here inherit class foo from above.
    
    </div>
    

    The current version of Fletcher's MultiMarkdown allows attributes to follow the link if using referenced links.

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

    If you just need a selector for Javascript purposes (like I did), you might just want to use a href attribute instead of a class or id:

    Just do this:

    <a href="#foo">Link</a>
    

    Markdown will not ignore or remove the href attribute like it does with classes and ids.

    So in your Javascript or jQuery you can then do:

    $('a[href$="foo"]').click(function(event) {
    
        ... do your thing ...
    
        event.preventDefault();
    });
    

    At least this works in my version of Markdown...

    0 讨论(0)
提交回复
热议问题