How to get only text from parsed eocument not the markdown code #975
-
I want to get only the text from a document. The nodes of a parsed document contain This is how the text is parsed, based on these docs.: $parser = new MarkdownParser(new Environment());
$doc = $parser->parse($this->getBody());
foreach ($doc->iterator() as $nod) {
if ($nod instanceof Text) {
echo 'Node: ' . get_class($nod) . ': ' . $nod->getLiteral() . "\n";
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The $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";
}
} |
Beta Was this translation helpful? Give feedback.
-
@colinodell ok, I see. I thought the default configuration would treat the input as markdown. |
Beta Was this translation helpful? Give feedback.
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 theCommonMarkCoreExtension
: