| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Markdown::Publish. | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # Full license text is available at: | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | # <http://dev.perl.org/licenses/> | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | package Markdown::Publish::Docusaurus; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Compiler pragma and package variables | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use strict qw(vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Parent and supporting packages | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Markdown::Publish (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Markdown::Publish::Constant; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use Cwd qw(abs_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use File::Path qw(make_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use JSON::PP qw(encode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Inheritance and version information | ||||||||||||||||||||||||||||
| 34 | # | ||||||||||||||||||||||||||||
| 35 | 1 | @ISA=qw(Markdown::Publish); | |||||||||||||||||||||||||||
| 36 | 1 | $VERSION='1.004'; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Done | ||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||
| 41 | 1 | 1; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | #====================================================================================================================== | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | ▶ | sub prepare { | ||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | my ($self)=@_; | |||||||||||||||||||||||||||
| 50 | 3 | my ($temporary_dn, $docs_dn, $pages_ar)=$self->prepare_docs(); | |||||||||||||||||||||||||||
| 51 | 3 | $self->promote_home($docs_dn, $pages_ar); | |||||||||||||||||||||||||||
| 52 | 3 | my $navigation_ar=$self->navigation($docs_dn, $pages_ar); | |||||||||||||||||||||||||||
| 53 | 3 | my $site_dn=File::Spec->catdir($temporary_dn, 'docusaurus'); | |||||||||||||||||||||||||||
| 54 | 3 | my $site_docs_dn=File::Spec->catdir($site_dn, 'docs'); | |||||||||||||||||||||||||||
| 55 | 3 | make_path($site_docs_dn); | |||||||||||||||||||||||||||
| 56 | 3 | $self->copy_tree($docs_dn, $site_docs_dn); | |||||||||||||||||||||||||||
| 57 | 3 | $self->normalize_node_markdown($site_docs_dn); | |||||||||||||||||||||||||||
| 58 | my @items=map {{ | ||||||||||||||||||||||||||||
| 59 | type => 'doc', | ||||||||||||||||||||||||||||
| 60 | id => $_->{'id'}, | ||||||||||||||||||||||||||||
| 61 | 12 | label => $_->{'title'} | |||||||||||||||||||||||||||
| 62 | 3 | }} @{$navigation_ar}; | |||||||||||||||||||||||||||
| 63 | 3 | my $version=$self->option('version', 'latest'); | |||||||||||||||||||||||||||
| 64 | 3 | my $package_hr={ | |||||||||||||||||||||||||||
| 65 | scripts => { | ||||||||||||||||||||||||||||
| 66 | build => 'docusaurus build', | ||||||||||||||||||||||||||||
| 67 | start => 'docusaurus start --no-open' | ||||||||||||||||||||||||||||
| 68 | }, | ||||||||||||||||||||||||||||
| 69 | dependencies => { | ||||||||||||||||||||||||||||
| 70 | '@docusaurus/core' => $version, | ||||||||||||||||||||||||||||
| 71 | '@docusaurus/preset-classic' => $version | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||
| 74 | 3 | $self->write_file(File::Spec->catfile($site_dn, 'package.json'), encode_json($package_hr)); | |||||||||||||||||||||||||||
| 75 | 3 | my ($config_fn, $extend_fn)=$self->configuration_files(); | |||||||||||||||||||||||||||
| 76 | 3 | my $prepared_config_fn; | |||||||||||||||||||||||||||
| 77 | 3 | ▶ | if (defined($config_fn) && length($config_fn)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $config_fn and length $config_fn
MC/DC50%1/2
defined $config_fn and length $config_fn
defined $config_fn
length $config_fn
Truth table66%2/3
A and B
A= defined $config_fnB= length $config_fn
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | die "Docusaurus configuration not found: $config_fn\n" unless -f $config_fn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | $prepared_config_fn=abs_path($config_fn); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||||||||||
| 82 | 2 | my $generated="{\n title: ". | |||||||||||||||||||||||||||
| 83 | encode_json($self->option('name', 'Documentation')). | ||||||||||||||||||||||||||||
| 84 | ",\n url: 'http://localhost',\n baseUrl: ". | ||||||||||||||||||||||||||||
| 85 | encode_json($self->site_base('/')). | ||||||||||||||||||||||||||||
| 86 | ",\n onBrokenLinks: 'warn',\n". | ||||||||||||||||||||||||||||
| 87 | " markdown: { format: 'detect' },\n". | ||||||||||||||||||||||||||||
| 88 | " presets: [['classic', { docs: { routeBasePath: '/', sidebarPath: require.resolve('./sidebars.js') }, blog: false }]],\n}"; | ||||||||||||||||||||||||||||
| 89 | 2 | my $config; | |||||||||||||||||||||||||||
| 90 | 2 | ▶ | if (defined($extend_fn)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | die "Docusaurus configuration extension not found: $extend_fn\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | unless -f $extend_fn; | ||||||||||||||||||||||||||||
| 93 | 1 | my $context_hr=$self->configuration_context($pages_ar, $navigation_ar); | |||||||||||||||||||||||||||
| 94 | 1 | my $authored_fn=abs_path($extend_fn); | |||||||||||||||||||||||||||
| 95 | 1 | $config="const generated = $generated;\n". | |||||||||||||||||||||||||||
| 96 | "const context = ".encode_json($context_hr).";\n". | ||||||||||||||||||||||||||||
| 97 | "const loaded = require(".encode_json($authored_fn).");\n". | ||||||||||||||||||||||||||||
| 98 | "const extend = loaded.default || loaded;\n". | ||||||||||||||||||||||||||||
| 99 | "if (typeof extend !== 'function') throw new Error('Docusaurus config_extend must export a function');\n". | ||||||||||||||||||||||||||||
| 100 | "const configured = extend(generated, context);\n". | ||||||||||||||||||||||||||||
| 101 | "if (configured && typeof configured.then === 'function') throw new Error('Docusaurus config_extend must be synchronous');\n". | ||||||||||||||||||||||||||||
| 102 | "if (!configured || typeof configured !== 'object' || Array.isArray(configured)) throw new Error('Docusaurus config_extend must return a configuration object');\n". | ||||||||||||||||||||||||||||
| 103 | "module.exports = configured;\n"; | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||
| 106 | 1 | $config="module.exports = $generated;\n"; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 2 | $prepared_config_fn=File::Spec->catfile($site_dn, 'docusaurus.config.js'); | |||||||||||||||||||||||||||
| 109 | 2 | $self->write_file($prepared_config_fn, $config); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 3 | $self->write_file(File::Spec->catfile($site_dn, 'sidebars.js'), | |||||||||||||||||||||||||||
| 112 | "module.exports = { docs: ".encode_json(\@items)." };\n"); | ||||||||||||||||||||||||||||
| 113 | 3 | return ($temporary_dn, $site_dn, $prepared_config_fn); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 24 | ▶ | sub normalize_backend_markdown { | ||||||||||||||||||||||||||
|
Subroutine
normalize_backend_markdown: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 24 | my ($self, $fn, $markdown)=@_; | |||||||||||||||||||||||||||
| 121 | 24 | return $self->title_frontmatter($fn, $markdown); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 246 | ▶ | sub heading_anchor_required { | ||||||||||||||||||||||||||
|
Subroutine
heading_anchor_required: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 246 | return 1; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub admonition_type { | ||||||||||||||||||||||||||
|
Subroutine
admonition_type: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | my ($self, $kind)=@_; | |||||||||||||||||||||||||||
| 136 | 0 | my %map=(note => 'note', tip => 'tip', warning => 'warning', | |||||||||||||||||||||||||||
| 137 | important => 'warning', caution => 'caution'); | ||||||||||||||||||||||||||||
| 138 | 0 | ▶ | return $map{$kind} || $kind; | ||||||||||||||||||||||||||
|
Condition0%0/3
$map{$kind} || $kind
MC/DC0%0/2
$map{$kind} || $kind
$map{$kind}
$kind
Truth table0%0/3
A || B
A= $map{$kind}B= $kind
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub build { | ||||||||||||||||||||||||||
|
Subroutine
build: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | my ($self)=@_; | |||||||||||||||||||||||||||
| 146 | 0 | my $output_dn=File::Spec->rel2abs($self->option('output', $MARKDOWN_PUBLISH_OUTPUT_DN)); | |||||||||||||||||||||||||||
| 147 | 0 | my (undef, $site_dn, $config_fn)=$self->prepare(); | |||||||||||||||||||||||||||
| 148 | 0 | $self->npm_install($site_dn); | |||||||||||||||||||||||||||
| 149 | 0 | $self->system_in_dir($site_dn, $self->option('npm', 'npm'), | |||||||||||||||||||||||||||
| 150 | 'run', 'build', '--', '--config', $config_fn, '--out-dir', $output_dn); | ||||||||||||||||||||||||||||
| 151 | 0 | return $output_dn; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 4 | ▶ | sub serve { | ||||||||||||||||||||||||||
|
Subroutine
serve: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 4 | my ($self)=@_; | |||||||||||||||||||||||||||
| 159 | 4 | my (undef, $site_dn, $config_fn)=$self->prepare(); | |||||||||||||||||||||||||||
| 160 | 4 | $self->npm_install($site_dn); | |||||||||||||||||||||||||||
| 161 | 4 | my $host=$self->option('host', $MARKDOWN_PUBLISH_HOST); | |||||||||||||||||||||||||||
| 162 | 4 | my $port=$self->option('port', $MARKDOWN_PUBLISH_PORT); | |||||||||||||||||||||||||||
| 163 | 4 | ▶ | $host='127.0.0.1' unless defined($host); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 4 | ▶ | $port=3001 unless defined($port); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 4 | return $self->system_in_dir($site_dn, $self->option('npm', 'npm'), | |||||||||||||||||||||||||||
| 166 | 'run', 'start', '--', '--config', $config_fn, | ||||||||||||||||||||||||||||
| 167 | '--host', $host, '--port', $port); | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | __END__ | ||||||||||||||||||||||||||||