TextformatterMarkdownExtra
Converts Markdown text to HTML using Parsedown, by Emanuil Rusev
A ProcessWire Textformatter module — apply it to any FieldtypeTextarea field in the
page editor, or call the markdown() method directly from code. Supports Parsedown Extra
features (footnotes, attribute syntax, definition lists, abbreviations, table of contents)
in addition to standard Markdown.
Inherited from Textformatter: format($str) and formatValue($page, $field, $value),
both of which ProcessWire calls automatically whenever this Textformatter is attached to a
field. This class adds a public markdown() helper plus configuration for the Markdown flavor
and safe mode.
// Apply to a field via the admin (Setup > Fields > your_field > Details, Textformatter).
// Or call directly:
$t = $modules->get('TextformatterMarkdownExtra');
$html = $t->markdown("# Headline
This is **bold** and _italic_.");
// <h1>Headline</h1><p>This is <strong>bold</strong> and <em>italic</em>.</p> Expand all Collapse all API reference
| Property | Type | Default | Description |
|---|---|---|---|
flavor | int | 2 | Bitmask selecting which Parsedown engine and extensions to use (see Constants below). |
safeMode | bool | false | When enabled, Parsedown escapes untrusted HTML in untrusted input (see safeMode()). |
You may pass any flavor constant directly to markdown() or getParsedown() to override
the module's configured flavor for a single call. The flavorParsedown bit (4) is an
ON/OFF switch — set it to choose Parsedown over ParsedownExtra. The flavorRCD bit
(16) is a modifier that may be OR-ed onto any base flavor to enable the deprecated
markdownExtensions() post-processing.
| Constant | Value | Description |
|---|---|---|
flavorDefault | 2 | Default flavor — Parsedown Extra. |
flavorParsedownExtra | 2 | Parsedown Extra — adds footnotes, abbreviations, definition lists, custom block classes, and more. |
flavorParsedown | 4 | Plain Parsedown — standard Markdown only, no extra syntax. |
flavorMarkdownExtra | 2 | Alias of flavorParsedownExtra (kept for backwards compatibility). |
flavorRCD | 16 | Optional modifier bit. OR with a base flavor to apply markdownExtensions() post-processing (@deprecated). |
format($str)
Format a string in place — the argument is passed by reference. Inherited from
Textformatter and implemented here simply by calling markdown() with the configured
flavor. ProcessWire invokes this automatically when the Textformatter is attached to a field
and output is rendered.
$str = "# Title
Hello **world**.";
$t->format($str);
echo $str;
// <h1>Title</h1><p>Hello <strong>world</strong>.</p>formatValue(Page $page, Field $field, $value)
Like format(), but with the owning Page and Field provided. The $value is modified
in place. Both Page and Field are ignored by this implementation — they are present only
to satisfy the Textformatter contract and are passed through to markdown(). Like
format(), ProcessWire calls this on your behalf when this Textformatter is attached to a
field and the field is output-formatted.
// You rarely call this yourself — ProcessWire handles it during output formatting:
$textformatter = $modules->get('TextformatterMarkdownExtra');
$textformatter->formatValue($page, $page->field, $page->getUnformatted('body')); markdown($str, $flavor = null, $safeMode = null)
Primary entry point — converts a Markdown string to HTML and returns it. All other formatting methods ultimately call this one.
- $str (
string) — Markdown text to convert. - $flavor (
int|null) — Flavor bitmask.nulluses the module's configuredflavor. PassflavorParsedown(4) for plain Markdown, or OR withflavorRCD(16) to apply the deprecated post-processing extensions on top of the default. - $safeMode (
bool|null) — Whentrue, escapes untrusted HTML in untrusted input.nulluses the module's configuredsafeModesetting. Pass an explicit bool to override the setting for this single call without changing the module state. - Returns
string— the converted HTML.
// Use configured flavor and safe mode:
$html = $t->markdown('# Hello');
// Plain Parsedown (no footnote support), bypassing safe mode for this call:
$html = $t->markdown('[^a]
[^a]: note', $t::flavorParsedown, false);
// <p>[^a]</p><p>[^a]: note</p>
// Default flavor (Parsedown Extra) renders footnotes:
$html = $t->markdown('[^a]
[^a]: note', $t::flavorParsedownExtra);
// <p><sup>...footnote link...</sup></p><div class="footnotes">…</div>markdownSafe($str, $flavor = 0)
Convenience wrapper around markdown() that forces safe mode on for this call. Equivalent to
markdown($str, $flavor, true). Unlike markdown(), the $flavor default is 0 (not null),
which always selects ParsedownExtra regardless of the module's configured flavor — pass an
explicit $flavor to override.
// Always escapes untrusted HTML, regardless of module's safeMode setting:
$html = $t->markdownSafe("[click](http://x.com)
<script>alert(1)</script>");
// <p><a href="http://x.com">click</a> <script>alert(1)</script></p>safeMode($safeMode = null)
Getter/setter for the module's persistent safeMode setting. When called with an argument,
stores true/false and returns the new value. When called with null (or no argument),
returns the current setting without modifying it. The setting takes effect on subsequent
calls to markdown() and getParsedown() that don't pass an explicit $safeMode.
$t->safeMode(true); // enable safe mode for future markdown() calls
$on = $t->safeMode(); // retrieve current setting (returns true)getParsedown($flavor = null)
Returns a fresh \ParsedownExtra or \Parsedown instance suitable for conversion. Behind
the scenes this require_once's the bundled parsedown and parsedown-extra PHP source
directories on first use. When the module's safeMode setting is enabled, the returned
instance has safe mode pre-applied ($parsedown->setSafeMode(true)). You normally don't
need to call this directly — markdown() does it for you — but it is useful if you want to
configure a Parsedown instance beyond what this module exposes.
- $flavor (
int|null) — Parsedown (flavorParsedown) or Parsedown Extra (default). - Returns
\ParsedownExtra|\Parsedown.
$p = $t->getParsedown($t::flavorParsedown); // plain \Parsedown
echo get_class($p); // 'Parsedown'
$p = $t->getParsedown(); // default
echo get_class($p); // 'ParsedownExtra'markdownExtensions($str) — @deprecated
Applies a set of "RCD" Markdown extensions after the text has been converted to HTML
(post-processing). Only invoked when flavorRCD is OR-ed onto the active flavor; reach it
directly only if you want to apply the same transforms to existing HTML. Treat this method as
deprecated — prefer standard Markdown/Extension syntax instead.
The post-processing adds:
- id attributes — a
#idimmediately after an HTML close tag addsid="..."to the preceding opening tag. - class attributes — a
.classimmediately after an HTML close tag addsclass="..."to the preceding opening tag. - target=_blank — a trailing
+after a Markdown link makes it open in a new window. - comment stripping — removes C-style
/* … */and line-start//comments from the converted HTML.
// Reachable directly (rarely needed):
$t->markdownExtensions($html);
// Or via the flavor flag, which markdown() calls internally:
$t->markdown($md, $t::flavorParsedownExtra | $t::flavorRCD);getModuleConfigInputfields(InputfieldWrapper $inputfields)
Implements ConfigurableModule. Provides the module configuration form shown in Setup > Modules > Markdown/Parsedown Extra:
- Markdown flavor to use — radio: Parsedown Extra (
flavorParsedownExtra) or Parsedown (flavorParsedown). - Safe mode? — toggle. When enabled, Parsedown escapes untrusted HTML in untrusted input. See the Parsedown security note.
- Test markdown — a textarea where you can paste Markdown, save the module config, and see the rendered HTML result as a system notice. Purely a testing convenience — no value is stored permanently.
You typically do not call this directly; ProcessWire invokes it when rendering module configuration.
- Access: Get an instance with
$modules->get('Textformatter. Like all Textformatters, it is singular (Markdown Extra') isSingular()returns true) — ProcessWire loads only one shared instance per request. It is not autoload — the module is not loaded until requested or until a field that uses it is rendered. - Applying to a field: In the admin, edit the field's Details settings and select this Textformatter from the Textformatters list. Multiple Textformatters can be chained.
- Safe mode and untrusted input: When the module's
safeModesetting (or the per-call argument tomarkdown()) is enabled, Parsedown escapes inline and block HTML rather than passing it through. Use this any time the Markdown source comes from untrusted users. - The
flavorRCDbit andmarkdownExtensions()are legacy. Prefer the native Parsedown Extra attribute syntax ({#id},{.class}) introduced by the library rather than the RCD post-processing syntax. __toString():Not overridden by this class; see WireData for inherited conversion behavior.- Source file:
wire/modules/Textformatter/TextformatterMarkdown Extra/Textformatter Markdown Extra.module.php - See also: Textformatter for the shared text formatter contract, FieldtypeTextarea for the field type where Textformatters are most commonly attached.
API reference: methods, properties
Markdown invented by John Gruber.
Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Textformatter class also inherits all the methods and properties of: Textformatter, WireData and Wire.
Common
Properties
| Name | Return | Summary | |
|---|---|---|---|
| Textformatter | int | Markdown flavor (see flavor constants) |
Additional methods and properties
In addition to the methods and properties above, Textformatter
API reference based on ProcessWire core version 3.0.269