Giter VIP home page Giter VIP logo

Comments (3)

miyuchina avatar miyuchina commented on May 27, 2024 1

I think I'm doing that only for HTMLSpan and not for HTMLBlock, in accordance with CommonMark. Part of the reason, and CommonMark is aware of this, is the performance cost when recursively rendering within HTML elements. The interesting thing is that CommonMark has some pretty particular definitions for HTMLBlock. Using these definitions, I think not rendering inner elements for HTMLBlock would be following the rule of least surprises. You can take a look at Section 4.6 in the spec in case you're interested.

Sorry for the late response! I'm closing the issue, but feel free to comment further.

from mistletoe.

miyuchina avatar miyuchina commented on May 27, 2024

I played with it a little, and perhaps this is what's going on. Using this code:

class Aside(SpanToken):
    pattern = re.compile(r"<aside>(.+?)</aside>")

class CustomRenderer(HTMLRenderer):
    def __init__(self):
        super().__init__(Aside)

    def render_aside(self, token):
        return '<aside>{}</aside>'.format(self.render_inner(token))

I can render:

bar <aside>*foo*</aside>

to:

<p>bar <aside><em>foo</em></aside></p>

But this markdown snippet will not render the inner emphasis:

<aside>*foo*</aside>

Because the entire block is first captured by the HTMLBlock token. To get around this, you need to define both a block-level aside tag and a span-level one, and pass both of them into the super constructor. Something like:

from mistletoe.span_token import tokenize_inner

class AsideBlock(BlockToken):
    def __init__(self, lines):
        # edit: strings are immutable! typo in the previous version of this reply
        lines[0] = lines[0].replace('<aside>', '')
        lines[-1] = lines[-1].replace('</aside>', '')
        super().__init__(''.join(lines), tokenize_inner)

    @classmethod
    def start(cls, line):
        return line.lstrip().startswith('<aside>')

    @classmethod
    def read(cls, lines):
        line_buffer = []
        for line in lines:
            line_buffer.append(line)
            if line.rstrip().endswith('</aside>') or line == '\n':
                break
        return line_buffer

By the way, rendering the inner elements of HTMLSpan should also be the standard behavior, and I'll probably add it soon. Let me know if there're any further questions!

from mistletoe.

joncass avatar joncass commented on May 27, 2024

Cool, this works. (Note: I did have to modify the AsideBlock init function a bit since strings are immutable so lines[0].replace returns a string but doesn't modify the given one). Thanks!

In terms of making it the default behavior to render inner elements, do you think you'd do that for both HTMLSpan and HTMLBlock or just one of them? I went back and forth on whether I wanted to do this for all elements or not - I think it probably makes sense, though I don't think it's strictly commonmark compliant.

from mistletoe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.