diff --git a/composer.json b/composer.json index 6d7597aa2..2d30d9adf 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,6 @@ "assetic/framework": "~3.0", "doctrine/dbal": "^2.6", "enshrined/svg-sanitize": "^0.15", - "erusev/parsedown-extra": "~0.7", "laravel/framework": "^9.1", "laravel/tinker": "^2.7", "league/csv": "~9.1", diff --git a/src/Parse/Markdown.php b/src/Parse/Markdown.php index 5146257d1..77206fd02 100644 --- a/src/Parse/Markdown.php +++ b/src/Parse/Markdown.php @@ -1,135 +1,448 @@ (Rewritten implementation using CommonMark) * - * Similarly, markdown.parse is fired after ParseDown has - * interpreted the (possibly modified) input. This event - * passes an array [$text, $data], where $text is the - * original unmodified Markdown input, and $data is the HTML - * code generated by ParseDown. - * - * @author Alexey Bobkov, Samuel Georges + * @method static enableAttributes() + * @method static enableAutolinking() + * @method static enableFootnotes() + * @method static enableFrontMatter() + * @method static enableHeadingPermalinks() + * @method static enableInlineOnly() + * @method static enableSafeMode() + * @method static enableTaskLists() + * @method static enableTables() + * @method static enableTableOfContents() + * @method static disableAttributes() + * @method static disableAutolinking() + * @method static disableFootnotes() + * @method static disableFrontMatter() + * @method static disableHeadingPermalinks() + * @method static disableInlineOnly() + * @method static disableSafeMode() + * @method static disableTaskLists() + * @method static disableTables() + * @method static disableTableOfContents() + * @method static \Winter\Storm\Parse\Markdown setAttributes(bool $enabled) + * @method static \Winter\Storm\Parse\Markdown setAutolinking(bool $enabled) + * @method static \Winter\Storm\Parse\Markdown setConfig(array $config) + * @method static \Winter\Storm\Parse\Markdown setFootnotes(bool $enabled) + * @method static \Winter\Storm\Parse\Markdown setFrontMatter(bool $enabled) + * @method static \Winter\Storm\Parse\Markdown setHeadingPermalinks(bool $enabled) + * @method static \Winter\Storm\Parse\Markdown setInlineOnly(bool $enabled) + * @method static \Winter\Storm\Parse\Markdown setSafeMode(bool $enabled) + * @method static \Winter\Storm\Parse\Markdown setTaskLists(bool $enabled) + * @method static \Winter\Storm\Parse\Markdown setTables(bool $enabled) + * @method static \Winter\Storm\Parse\Markdown setTableOfContents(bool $enabled) **/ class Markdown { use \Winter\Storm\Support\Traits\Emitter; /** - * @var string Parsedown class + * Enables the parsing of attributes for block-level and inline content. */ - protected $parserClass = \Winter\Storm\Parse\Parsedown\Parsedown::class; + public bool $attributes = false; /** - * Gets an instance of the parser. - * - * We return a new instance each time to prevent contamination of clean instances. - * - * @return Parsedown + * Enables autolinking of URLs and email addresses in Markdown content. + */ + public bool $autolinking = true; + + /** + * Enables the parsing and generation of footnotes. + */ + public bool $footnotes = false; + + /** + * Enables the parsing of front matter (metadata). + */ + public bool $frontMatter = false; + + /** + * Enables the generation of permalinks for each heading. + */ + public bool $headingPermalinks = false; + + /** + * Enables inline-only formatting. This is used for rendering a single line of Markdown. + */ + public bool $inlineOnly = false; + + /** + * Enables safe mode - disables certain HTML tags. + */ + public bool $safeMode = false; + + /** + * Enables the parsing and generation of task lists. + */ + public bool $taskLists = false; + + /** + * Enables the generation of tables. + */ + public bool $tables = true; + + /** + * Enables the generation of a table of contents. + */ + public bool $tableOfContents = false; + + /** + * Custom configuration for the CommonMark environment. + */ + public array $config = []; + + /** + * Extracted front matter (metadata) from the Markdown content. */ - protected function getParser() + protected ?array $frontMatterData = null; + + /** + * The Markdown parser class to use. + */ + protected ?string $parserClass = null; + + /** + * The HTML renderer class to use. + */ + protected ?string $rendererClass = null; + + /** + * Constructor. + */ + final public function __construct() { - return new $this->parserClass; } /** - * Sets the Markdown parser. - * - * @param string|object $parserClass - * @return void + * Parse Markdown content and render as HTML. */ - public function setParser(string|object $parserClass) + public function parse(string $markdown): string { - if (is_object($parserClass)) { - $this->parserClass = get_class($parserClass); - } else { - $this->parserClass = $parserClass; + $environment = $this->createEnvironment(); + + if ($this->frontMatter) { + $markdown = $this->extractFrontMatter($markdown); } + + return $this->parseInternal($environment, $markdown); } /** - * Parse text using Markdown and Markdown-Extra - * @param string $text Markdown text to parse - * @return string Resulting HTML + * Parse Markdown content and render as HTML, with unsafe tags disabled. + * + * This will prevent tags such as