Skip to content

How to add custom html tags #514

Closed
Closed

Description

@lordfriend

I want to add some custom html tags like this:

<example>
    <file name="index.html">
    ```html
      <div class="example">
      example code here
      </div>
    ```
    </file>
</example>

but it seems <example> and <file> tag are not recognised as a valid html tag. marked think they are paragraph elements and add a couple of <p> around them

the compiled content is:

<p><example>
    <file name="index.html"></p>
    <pre><code class="lang-html">
      &lt;div class="example"&gt;
      example code here
      &lt;/div&gt;
    </code></pre>
    <p></file>
</example></p>

This behave make the <example> and <file> tag fail to work

Activity

  1. lordfriend commented on Nov 19, 2014

    @lordfriend
    Author

    Well,
    when I added some lines between the custom html tag and code blocks, it seems get worked. but the code blocks failed.

  2. brunowego commented on Mar 10, 2016

    @brunowego

    This is very inconvenient, I am creating a documentation for my project which involves displaying the element, however unfortunately is automatically added a paragraph around.

    My Angular directive:

    <pf-panel>
        <pf-body>
            <p>Exemplo de painel com corpo.</p>
        </pf-body>
    </pf-panel>

    The results:

    <p><pf-panel>
        <pf-body>
            <p>Exemplo de painel com corpo.</p>
        </pf-body>
    </pf-panel></p>

    Perhaps not the purpose of this project ...

  3. brunowego commented on Mar 10, 2016

    @brunowego

    @chjj if you have a little time, please take a look on this issue 👍

  4. joshbruce commented on Dec 25, 2017

    @joshbruce
    Member
  5. ghdoergeloh commented on Nov 30, 2020

    @ghdoergeloh

    Sorry to comment on this old issue, but I cannot find what happend to this issue/feature request.
    I found some Issues like #283 and the PR #833 but that was not merged.
    I still have the problem, that custom html tags are not recognised by the parser. Why aren't they handled like normal html?

  6. ghdoergeloh commented on Nov 30, 2020

    @ghdoergeloh

    Okay, partially it is working:

    > marked.parse('<div-test></div-test>') // didn't work
    '<p><div-test></div-test></p>\n'
    
    > marked.parse('<div-test>\n</div-test>') // worked
    '<div-test>\n</div-test>'
    
    > marked.parse('<div-test>test</div-test>') // didn't work
    '<p><div-test>test</div-test></p>\n'
    
    > marked.parse('<div-test>\ntest\n</div-test>') // worked
    '<div-test>\ntest\n</div-test>'
    
  7. UziTech commented on Nov 30, 2020

    @UziTech
    Member

    That is because there is a difference between an HTML block and inline HTML.

    Any inline element will get wrapped by a <p> tag if it is not inside another block element. You can use parseInline to prevent wrapping if you only want inline blocks parsed.

  8. bent10 commented on Oct 26, 2023

    @bent10
    Contributor

    It's super late to join this issue, but I'd like to share an alternative approach that utilizes directive syntax which might be suitable for your needs:

    import { Marked } from 'marked'
    import { createDirectives, presetDirectiveConfigs } from 'marked-directive'
    
    const markdown = `::::example{#foo.bar}
    :::file{name="index.html"}
    \`\`\`html
    <div class="example">
      <p>example code here</p>
    </div>
    \`\`\`
    :::
    ::::
    `
    
    const html = new Marked()
      .use(
        createDirectives([
          ...presetDirectiveConfigs,
          { level: 'container', marker: '::::' }
        ])
      )
      .parse(markdown)
    
    console.log(html)

    Yields:

    <example id="foo" class="bar">
      <file name="index.html">
        <pre><code class="language-html">&lt;div class=&quot;example&quot;&gt;
      &lt;p&gt;example code here&lt;/p&gt;
    &lt;/div&gt;
    </code></pre>
      </file>
    </example>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions