Skip to content

How to get only text from parsed eocument not the markdown code #975

Answered by colinodell
robopzet asked this question in Q&A
Discussion options

You must be logged in to vote

The new Environment() in your code isn't configured with any parsers or extensions, and therefore the engine doesn't know how to parse those tokens, so it assumes everything it sees is plain text. Try adding the CommonMarkCoreExtension:

$environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension());

$parser = new MarkdownParser($environment);
$doc    = $parser->parse($this->getBody());

foreach ($doc->iterator() as $nod) {
    if ($nod instanceof Text) {
        echo 'Node: ' . get_class($nod) . ': ' . $nod->getLiteral() . "\n";
    }
}

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by colinodell
Comment options

You must be logged in to vote
1 reply
@colinodell
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants