Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
with:
node-version: "18"
- run: npm install -g yarn && yarn install
- run: apt-get update && apt-get -y install libcmark-dev
- name: Install Carton
uses: perl-actions/install-with-cpm@stable
with:
Expand Down
2 changes: 1 addition & 1 deletion cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ requires 'Catalyst::TraitFor::Request::REST';
requires 'Catalyst::View::JSON';
requires 'Catalyst::View::Xslate';
requires 'CatalystX::Fastly::Role::Response', '0.06';
requires 'CommonMark';
requires 'Config::General';
requires 'Config::ZOMG', '1.000000';
requires 'Cpanel::JSON::XS';
Expand Down Expand Up @@ -69,7 +70,6 @@ requires 'Plack::Test';
requires 'Ref::Util', '>= 0.008';
requires 'Router::Simple';
requires 'Term::Size::Any';
requires 'Text::MultiMarkdown';
requires 'Text::Pluralize';
requires 'Text::Xslate::Bridge';
requires 'Text::Xslate::Bridge::Star';
Expand Down
45 changes: 19 additions & 26 deletions cpanfile.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,15 @@ DISTRIBUTIONS
perl 5.006
strict 0
warnings 0
CommonMark-0.290000
pathname: N/NW/NWELLNHOF/CommonMark-0.290000.tar.gz
provides:
CommonMark 0.290000
CommonMark::Node 0.290000
requirements:
Devel::CheckLib 0
ExtUtils::MakeMaker 0
perl 5.008
Config-Any-0.33
pathname: H/HA/HAARG/Config-Any-0.33.tar.gz
provides:
Expand Down Expand Up @@ -1482,6 +1491,16 @@ DISTRIBUTIONS
Test::More 0.98
parent 0
perl 5.008001
Devel-CheckLib-1.16
pathname: M/MA/MATTN/Devel-CheckLib-1.16.tar.gz
provides:
Devel::CheckLib 1.16
requirements:
Exporter 0
ExtUtils::MakeMaker 0
File::Spec 0
File::Temp 0.16
perl 5.004050
Devel-Confess-0.009004
pathname: H/HA/HAARG/Devel-Confess-0.009004.tar.gz
provides:
Expand Down Expand Up @@ -5440,32 +5459,6 @@ DISTRIBUTIONS
Exporter 0
ExtUtils::MakeMaker 0
perl 5.006
Text-Markdown-1.000031
pathname: B/BO/BOBTFISH/Text-Markdown-1.000031.tar.gz
provides:
Text::Markdown 1.000031
requirements:
Digest::MD5 0
Encode 0
ExtUtils::MakeMaker 6.42
FindBin 0
List::MoreUtils 0
Test::Differences 0
Test::Exception 0
Test::More 0.42
Text::Balanced 0
Text-MultiMarkdown-1.002
pathname: B/BD/BDFOY/Text-MultiMarkdown-1.002.tar.gz
provides:
Text::MultiMarkdown 1.002
requirements:
Digest::MD5 0
Encode 0
ExtUtils::MakeMaker 6.64
File::Spec::Functions 0
HTML::Entities 0
Text::Markdown v1.0.26
perl 5.008
Text-Pluralize-1.1
pathname: K/KV/KVAIL/Text-Pluralize-1.1.tar.gz
provides:
Expand Down
4 changes: 2 additions & 2 deletions lib/MetaCPAN/Web/Controller/Feed.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use HTML::Escape qw( escape_html );
use MetaCPAN::Web::Types qw( ArrayRef Enum HashRef Str Undef Uri DateTime );
use Params::ValidationCompiler qw( validation_for );
use Path::Tiny qw( path );
use Text::MultiMarkdown qw( markdown );
use MetaCPAN::Web::RenderUtil qw( render_markdown );
use URI ();
use XML::FeedPP ();

Expand Down Expand Up @@ -100,7 +100,7 @@ sub news : Private {

#$str =~ s{\[([^]]+)\]\(([^)]+)\)}{<a href="$2">$1</a>}g;
$e{abstract} = $str;
$e{abstract} = markdown($str);
$e{abstract} = render_markdown($str);

push @entries, \%e;
}
Expand Down
64 changes: 64 additions & 0 deletions lib/MetaCPAN/Web/RenderUtil.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use HTML::Escape qw( escape_html );
use HTML::Restrict ();
use URI ();
use Digest::MD5 ();
use CommonMark qw( :node :event );

our @EXPORT_OK = qw(
filter_html
gravatar_image
render_markdown
split_index
);

Expand Down Expand Up @@ -161,4 +163,66 @@ sub split_index {
return ( $pod_index, $html );
}

my @is_leaf;
$is_leaf[$_] = 1
for (
NODE_HTML, NODE_HRULE, NODE_CODE_BLOCK, NODE_TEXT,
NODE_SOFTBREAK, NODE_LINEBREAK, NODE_CODE, NODE_INLINE_HTML,
);

sub render_markdown {
my ($markdown) = @_;

my $doc = CommonMark->parse_document($markdown);

my ( $html, $header_content, %seen_header );

my $iter = $doc->iterator;
while ( my ( $ev_type, $node ) = $iter->next ) {
my $node_type = $node->get_type;

if ( $node_type == NODE_DOCUMENT ) {
next;
}

if ( $node_type == NODE_HEADER ) {
if ( $ev_type == EVENT_ENTER ) {
$header_content = '';
}
if ( $ev_type == EVENT_EXIT ) {
$header_content =~ s{(?:-(\d+))?$}{'-' . (($1 // 1) + 1)}e
while $seen_header{$header_content}++;

my $header_html = $node->render_html;
$header_html
=~ s/^<h[0-9]+\b\K/' id="'.escape_html($header_content).'"'/e;
$html .= $header_html;

undef $header_content;
}
}
elsif ($ev_type == EVENT_ENTER
&& $node->parent->get_type == NODE_DOCUMENT )
{
$html .= $node->render_html;
}

if ( defined $header_content ) {
if ( $is_leaf[$node_type] ) {
my $content = lc( $node->get_literal );
$content =~ s/\A\s+//;
$content =~ s/\s+\z//;
$content =~ s/\s+/-/g;

if ( length $content ) {
$header_content .= '-' if length $header_content;
$header_content .= $content;
}
}
}
}

return $html;
}

1;
6 changes: 2 additions & 4 deletions lib/MetaCPAN/Web/View/Xslate/Bridge.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use parent qw(Text::Xslate::Bridge);
use Text::Xslate::Util qw( mark_raw );
use Number::Format ();
use Ref::Util qw( is_coderef is_regexpref );
use Text::MultiMarkdown ();
use List::Util ();
use DateTime ();
use With::Roles ();
use Text::Pluralize ();
use MetaCPAN::Web::RenderUtil qw( gravatar_image ); ## no perlimports
use MetaCPAN::Web::RenderUtil qw( render_markdown );
use overload ();

my $num_formatter = Number::Format->new;
Expand All @@ -26,11 +26,9 @@ sub format_bytes {
$num_formatter->format_bytes($number);
}

my $md = Text::MultiMarkdown->new( heading_ids => 1 );

sub markdown {
my ($text) = @_;
mark_raw( $md->markdown($text) );
mark_raw( render_markdown($text) );
}

sub filter_html {
Expand Down
32 changes: 32 additions & 0 deletions t/markdown.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use strict;
use warnings;
use Test::More;

use MetaCPAN::Web::RenderUtil qw( render_markdown );

my $html = render_markdown(<<'EOM');
# Heading

Some body text

## Heading

More stuff

## Heading

more stuff

## Heading **with** _markup_

Content

EOM

like $html, qr{<h1 id="heading">Heading</h1>}, 'first heading';
like $html, qr{<h2 id="heading-2">Heading</h2>}, 'second heading';
like $html, qr{<h2 id="heading-3">Heading</h2>}, 'third heading';

like $html, qr{<h2 id="heading-with-markup">Heading <}, 'heading with markup';

done_testing();