Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[markdown]: shortcodes in posts should not be wrapped in <p> #126

Open
thelevking opened this issue Jul 21, 2021 · 1 comment
Open

[markdown]: shortcodes in posts should not be wrapped in <p> #126

thelevking opened this issue Jul 21, 2021 · 1 comment

Comments

@thelevking
Copy link

Elder.js' Markdown plugin wraps any shortcode placed on its own line in <p></p>, including any Svelte component called via {{svelteComponent name='...' /}}. This creates invalid HTML if any non-inline tags are wrapped into <p>. Elder.js' Markdown parser should remove these

tags from the resulting HTML.

@markjaquith
Copy link
Contributor

markjaquith commented Sep 27, 2021

Here's how I've worked around this for now:

In shortcodes.js, I mark any shortcodes I want to be "block level" with block: true.

Then in hooks.js I have this:

  {
    hook: 'shortcodes',
    name: 'unwrapBlockShortcodes',
    description: 'Looks for block level shortcodes and remove their surrounding paragraph tags',
    priority: 99,
    run: async ({ layoutHtml, shortcodes }) => {
      const blockShortcodes = shortcodes.filter(shortcode => shortcode.block)
      let newHtml = layoutHtml
      blockShortcodes.forEach(({ shortcode }) => {
        newHtml = newHtml
          .replace(new RegExp(`<p>({{${shortcode}( .*)?/?}})(</p>)?`, 'g'), '$1')
          .replace(new RegExp(`({{/${shortcode}}})</p>`, 'g'), '$1')
      })

      return {
        layoutHtml: newHtml
      }
    },
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants