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

heading token content is always the last heading #212

Open
clehene opened this issue Feb 22, 2024 · 1 comment
Open

heading token content is always the last heading #212

clehene opened this issue Feb 22, 2024 · 1 comment

Comments

@clehene
Copy link

clehene commented Feb 22, 2024

The content for block_token.Heading gets har

import mistletoe
from mistletoe import Document

markdown_text = """
## Heading A
Some text under heading 1.
## Heading B *italic*
Some text under heading 2.
## Heading C **bold**
Some text under heading 3.
"""

# Parse the Markdown text

doc = Document(markdown_text)

for token in doc.children:
    if isinstance(token, mistletoe.block_token.Heading):
        # heading_content = " ".join(str(child.content) for child in token.children if hasattr(child, 'content'))
        heading_content = token.content
        print(f'Level: {token.level}, Content: {heading_content}')

will output

Level: 2, Content: Heading C **bold**
Level: 2, Content: Heading C **bold**
Level: 2, Content: Heading C **bold**
@pbodnar
Copy link
Collaborator

pbodnar commented Feb 24, 2024

Hi @clehene, thanks for the report. For elements like BlockCode or HtmlBlock, the content property/attribute makes sense (see #163). For others, like Heading, not so much.

As you are not the first to ask this question (see #99), I think the simplest thing to do now is to rename the reported Heading's content class attribute to _content, so that it is clear that it is not a part of the public API. That would be in-line e.g. with _open_info in the CodeFence token class.

I can also imagine we could introduce a getter property called text or similar that would recursively concat text from the child tokens, but that would be for another feature request...

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

No branches or pull requests

2 participants