NAME

Markdown::Perl – Very configurable Markdown processor written in pure Perl, supporting the CommonMark spec and many extensions.

SYNOPSIS

This is the library underlying the pmarkdown tool. It can be used in an object-oriented style:

use Markdown::Perl;
my $converter = Markdown::Perl->new([mode => $mode], %options);
my $html = $converter->convert($markdown);

Or the library can be used functionally:

use Markdown::Perl 'convert';
Markdown::Perl::set_options([mode => $mode], %options);
my $html = convert($markdown);

DESCRIPTION

new

my $pmarkdown = Markdown::Perl->new([mode => $mode], %options);

Creates a Markdown::Perl object that can be used to convert Markdown data into HTML. Note that you don’t have to create an instance of this class to use this module. The methods of this class can also be used like package functions and they will operate on an implicit default instance of the class.

See the "MODES" in pmarkdown page for the documentation of existing modes.

See the Markdown::Perl::Options documentation for all the existing options.

For the reference on the default syntax supported by the library, see the GitHub repository of the project: https://github.com/mkende/pmarkdown/blob/main/Syntax.md

set_options

$pmarkdown->set_options(%options);
Markdown::Perl::set_options(%option);

Sets the options of the current object or, for the functional version, the options used by functional calls to convert(). The options set through the functional version do not apply to any objects created through a call to new().

See the Markdown::Perl::Options documentation for all the existing options.

set_mode

$pmarkdown->set_mode($mode);
Markdown::Perl::set_mode($mode);

Specifies a mode for the current object or, for the functional version, the mode used by functional calls to convert(). A mode is a set of configuration options working together, typically to replicate the semantics of another existing Markdown processor. See the "MODES" in pmarkdown documentation for a list of available modes.

When a mode is applied, it sets specific values for some options but any value for these options set through the set_options() will take precedence, even if set_options() is called before set_mode(). The mode set through the functional version does not apply to any objects created through a call to new().

convert

my $html = $pmarkdown->convert($md);
my $html = Markdown::Perl::convert($md);

Converts the given $md string into HTML. The input string must be a decoded Unicode string (or an ASCII string) and the output is similarly a decoded Unicode string.

set_hooks

$pmarkdown->set_hooks(hook_name => sub { ... }, ...);
Markdown::Perl::set_hooks(hook_name => sub { ... }, ...);

Registers hooks to be called during the processing of a Markdown document. Each hook must point to a code reference. You can remove a hook by passing undef as the value associated with a hook.

There is currently a single hook:

  • resolve_link_ref: this hook will receive a single string an argument, containing the label of a link reference in case where there was no matching link definition in the document and it must returns either undef or a hash-reference containing a target key, pointing to the link destination and optionally a title key containing the title of the key. The hash-ref can also contain a content key, in which case its value should be a span of HTML which will replace whatever would have been used for the link content.

  • yaml_metadata: this hook will trigger if there is valid YAML metadata at the beginning of the file. This also requires that the parse_file_metadata option is set to yaml (which is the default in our default mode). The hook will receive a hashref or an arrayref with the content of the YAML document.

    To allow conditional parsing based on some metadata, if the hook dies, the exception will interrupt the processing and be propagated as-is to the initial caller.

AUTHOR

Mathias Kende <mathias@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2024 Mathias Kende

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

SEE ALSO

pmarkdown
Text::Markdown another pure Perl implementation, implementing the original Markdown syntax from http://daringfireball.net/projects/markdown.
CommonMark a wrapper around the official CommonMark C library.