Skip to content

Comments

Add support for markdown script children#29

Merged
zerodevx merged 2 commits intozerodevx:masterfrom
bennypowers:patch-2
Sep 20, 2020
Merged

Add support for markdown script children#29
zerodevx merged 2 commits intozerodevx:masterfrom
bennypowers:patch-2

Conversation

@bennypowers
Copy link

@bennypowers bennypowers commented May 23, 2020

(apologies for omitting description and title up till now)

This PR adds support for markdown script children in addition to XMP:

<zero-md>
  <script type="text/markdown">
    # This is my markdown script
    It is automatically dedented
      - so this line, with a src indent of 6, will be parsed as having a markdown indent of 2
  </script>
</zero-md>

xmp template children will take precedence over script tags

queries for script elements with [markdown mimetype](https://tools.ietf.org/html/rfc7763) type attr
dedents the inputs to authors can write pretty HTML
@bennypowers bennypowers changed the title Patch 2 Add support for markdown script children Jun 9, 2020
@bennypowers
Copy link
Author

👋 Is this PR of interest?

@zerodevx
Copy link
Owner

Hi! Thanks for the PR! Sorry about the delay - I just saw this =/

Could you tell me more about the whitespace issue (if we stick to xmp tags)?

@bennypowers
Copy link
Author

With xmp tags, whitespace is preserved
so

<html>
  <body>
    <main>
      <article>
        <details>
          <summary>Show Code</summary>
          <zero-md>
            <xmp>
              Whitespace was preserved :/
              So these  two lines are markdown blockquotes

in this PR, script tags are dedented, so authors can write inline markdown without worrying about the initial indent

<html>
  <body>
    <main>
      <article>
        <details>
          <summary>Show Code</summary>
          <zero-md>
            <script type="text/markdown">
              Whitespace was dedented :)

              So this line is a markdown paragraph

                  And this line is a blockquote

Besides the whitespace, your README (and MDN) notes that XMP tags are deprecated. <script type="text/markdown> represents markdown in the most semantically correct way, since it's internet media type is text/markdown, and since when used with zero-md, the markdown source is not meant to be read as text by the reader, but rather parsed into HTML and displayed via the DOM, <script> is correct.

@zerodevx
Copy link
Owner

Thanks for the detailed explanation - this PR is definitely of interest. I generally agree that using <script type="text/markdown"> is probably more semantically correct.

I see that you have a dedent function with a bunch of regex. Could you walk me through that too?

Also, since <style> tags are required to be wrapped with <template>, does it make sense that the <script> tag should be child of <template> as well so that usage is consistent?

@bennypowers
Copy link
Author

Sure, So the dedent regex is adapted from various dedent implementations around the web. Maybe it's a little dark art (I've seen worse lol), but we're basically accumulating the "initial-whitespace-from-line-start" (/^\s+/) in the form of a regexp, then just removing it from the rest of the string - just considering the first instance of an indented line to be canonical to the rest of the string, which maps with the use case I outlined above. We could maybe do that with list operations on string and .replaceAll, but I doubt that would be as performant, and replaceAll is not widely implemented.

I'm not married to this particular implementation, but it has the advantage of being small and working. I haven't found an example of breaking input, but I haven't looked particularly hard 😉

WRT <template>, a normal <style> tag would apply its styles to the document (or shadow root), hence the need to wrap in <template> in order to prevent parsing. <script type="text/markdown"> is inherently inert, so shouldn't need to be wrapped.

Maybe in the future browsers will decide to implement native support for <script type="text/markdown">, which would I guess break this PR? But that seems like an incredibly unlikely scenario, and anyways if that were the case, this element would probably be obsolete anyways.

@bennypowers
Copy link
Author

Just checking in to see if this meets your approval

@zerodevx
Copy link
Owner

I'll get it settled this weekend!! Sorry!!

@zerodevx zerodevx merged commit 8c0a02a into zerodevx:master Sep 20, 2020
@zerodevx
Copy link
Owner

Merged! Thanks for your contribution! 👍

@bennypowers
Copy link
Author

Outstanding. Thanks!

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

Successfully merging this pull request may close these issues.

2 participants