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

Surprising Heading.content #99

Closed
PatrickMassot opened this issue Jul 1, 2020 · 4 comments
Closed

Surprising Heading.content #99

PatrickMassot opened this issue Jul 1, 2020 · 4 comments

Comments

@PatrickMassot
Copy link

In the following code, I was expecting to see my two titles printed.

from mistletoe import Document                                                            
                                                                                                         
doc = Document("""# My first title                                                        
Some text.                                                                                               
                                                                                          
# My second title                                                                         

Some more text.
""")                                                                                                     
                                                                                          
print(doc.children[0].content, doc.children[2].content)

But this prints the second title twice. To get the result I expected, I need to write print(doc.children[0].children[0].content, doc.children[2].children[0].content). I don't know if this is a bug or expected behavior, but I justed wasted half an hour on this.

@pbodnar
Copy link
Collaborator

pbodnar commented Jul 5, 2020

Hi, this really is the expected behavior. Because by writing doc.children[0].content you access a somewhat-private, class-scoped helper property content of the Heading class. So yes, accessing the parsed text by doc.children[0].children[0].content instead is the correct way.

If you want to know how to access the parsed structure, the best is probably to run mistletoe with the ASTRenderer against your markdown text. With this, you'll get a nice tree view of what got parsed:

{
  "type": "Document",
  "footnotes": {},
  "children": [
    {
      "type": "Heading",
      "level": 1,
      "children": [
        {
          "type": "RawText",
          "content": "My first title"
        }
      ]
    },
    {
      "type": "Paragraph",
      "children": [
        {
          "type": "RawText",
          "content": "Some text."
        }
      ]
    },
    {
      "type": "Heading",
      "level": 1,
      "children": [
        {
          "type": "RawText",
          "content": "My second title"
        }
      ]
    },
    {
      "type": "Paragraph",
      "children": [
        {
          "type": "RawText",
          "content": "Some more text."
        }
      ]
    }
  ]
}

(BTW You should remove trailing spaces from your example markdown code - or one gets Code blocks parsed otherwise.)

@PatrickMassot
Copy link
Author

Ok, thanks for the explanation. Feel free to close this issue, but I think some more documentation would be helpful here. I don't think it is such an exotic thing to do (I wanted to make the first h1 into the title of the whole thing in some metadata associated to the file).

@pbodnar
Copy link
Collaborator

pbodnar commented Jul 6, 2020

@PatrickMassot, you're welcome. :) I'm not this project's maintainer though, so I can't close this issue. Anyway, I agree with you this could be documented, probably on the Contributing page...

@pbodnar
Copy link
Collaborator

pbodnar commented Oct 30, 2021

Now when I can contribute to the project directly, I added a corresponding chapter Understanding the AST in 14a0d9e. I hope that could suffice. Suggestions welcome. :)

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