Publish/VitePress.pm

filter: Statementstmt 88.3% 68 / 77 Branchbran 92.8% 13 / 14 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 4 totaltotal 80.5% 95 / 118 CC 10 SCAR 13.0
CC
10
Cov
86%
prepare
7.0
admonition_type
6.0
serve
3.0
CRAP 3.7
« Publish/Starlight.pm ↑ Summary Publish/Docusaurus.pm »
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::VitePress;
14  
15  
16 # Compiler pragma and package variables
17 #
18 2 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 # Parent and supporting packages
24 #
25 2 ▶use Markdown::Publish ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Markdown::Publish::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use JSON::PP qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32  
33 # Inheritance and version information
34 #
35 2 @ISA=qw(Markdown::Publish);
36 2 $VERSION='1.004';
37  
38  
39 # Done
40 #
41 2 1;
42  
43  
44 #======================================================================================================================
45  
46  
47 6 ▶sub prepare {
Subroutine prepare: called CC=7 SCAR=19.5
Pod undocumented
48  
49 6 my ($self)=@_;
50 6 my ($temporary_dn, $docs_dn, $pages_ar)=$self->prepare_docs();
51 6 $self->promote_home($docs_dn, $pages_ar);
52 6 my $navigation_ar=$self->navigation($docs_dn, $pages_ar);
53 6 $self->normalize_node_markdown($docs_dn);
54 6 my $version=$self->option('version', 'latest');
55 6 my $package_hr={
56 type => 'module',
57 dependencies => {vitepress => $version}
58 };
59 6 $self->write_file(File::Spec->catfile($temporary_dn, 'package.json'),
60 encode_json($package_hr));
61 6 my $config_dn=File::Spec->catdir($docs_dn, '.vitepress');
62 6 make_path($config_dn);
63 6 my ($config_fn, $extend_fn)=$self->configuration_files();
64 5 my $prepared_config_fn;
65 5 ▶ if (defined($config_fn) && length($config_fn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_fn and length $config_fn) { } 1 4 5
Condition66%2/3
defined $config_fn and length $config_fn
!ll&&!rl&&r
401
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
ABresult
0X0
100
111
66 1 ▶ die "VitePress configuration not found: $config_fn\n" unless -f $config_fn;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $config_fn 0 1 1
67 1 $prepared_config_fn=abs_path($config_fn);
68 }
69 else {
70 my @items=map {
71 18 ▶ my $link=$_->{'id'} eq 'index' ? '/' : '/'.$_->{'id'};
Branch: 1 branch
BranchTrueFalseTotal
$_->{'id'} eq 'index' ? : 4 14 18
72 18 " { text: ".encode_json($_->{'title'}).
73 ", link: ".encode_json($link)." }"
74 4 } @{$navigation_ar};
75 4 my $generated="{\n title: ".
76 encode_json($self->option('name', 'Documentation')).
77 ",\n base: ".encode_json($self->site_base('/')).
78 ",\n themeConfig: {\n sidebar: [\n".
79 join(",\n", @items)."\n ]\n }\n}";
80 3 my $config;
81 3 ▶ if (defined($extend_fn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extend_fn) { } 2 1 3
82 2 ▶ die "VitePress configuration extension not found: $extend_fn\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $extend_fn 1 1 2
83 unless -f $extend_fn;
84 1 my $context_hr=$self->configuration_context($pages_ar, $navigation_ar);
85 1 my $authored_fn=abs_path($extend_fn);
86 1 $config="import { pathToFileURL } from 'node:url';\n\n".
87 "const generated = $generated;\n".
88 "const context = ".encode_json($context_hr).";\n".
89 "const extend = (await import(pathToFileURL(".
90 encode_json($authored_fn).").href)).default;\n".
91 "if (typeof extend !== 'function') throw new Error('VitePress config_extend must export a default function');\n".
92 "const configured = await extend(generated, context);\n".
93 "if (!configured || typeof configured !== 'object' || Array.isArray(configured)) throw new Error('VitePress config_extend must return a configuration object');\n".
94 "export default configured;\n";
95 }
96 else {
97 1 $config="export default $generated;\n";
98 }
99 2 $prepared_config_fn=File::Spec->catfile($config_dn, 'config.mts');
100 2 $self->write_file($prepared_config_fn, $config);
101 }
102 3 return ($temporary_dn, $docs_dn, $prepared_config_fn);
103  
104 }
105  
106  
107 0 ▶sub admonition_type {
Subroutine admonition_type: not called CC=2 SCAR=17.9
Pod undocumented
108  
109 0 my ($self, $kind)=@_;
110 0 my %map=(note => 'info', tip => 'tip', warning => 'warning',
111 important => 'warning', caution => 'warning');
112 0 ▶ return $map{$kind} || $kind;
Condition0%0/3
$map{$kind} || $kind
l!l&&r!l&&!r
000
MC/DC0%0/2
$map{$kind} || $kind
$map{$kind} $kind
Truth table0%0/3
A || B
A= $map{$kind}B= $kind
ABresult
1X1
011
000
113  
114 }
115  
116  
117 0 ▶sub build {
Subroutine build: not called CC=1 SCAR=6.9
Pod undocumented
118  
119 0 my ($self)=@_;
120 0 my $output_dn=File::Spec->rel2abs($self->option('output', $MARKDOWN_PUBLISH_OUTPUT_DN));
121 0 my ($temporary_dn, $docs_dn, $config_fn)=$self->prepare();
122 0 $self->npm_install($temporary_dn);
123 0 $self->system_in_dir($temporary_dn, $self->option('npm', 'npm'),
124 'exec', '--', 'vitepress', 'build', $docs_dn, '--config', $config_fn,
125 '--outDir', $output_dn);
126 0 return $output_dn;
127  
128 }
129  
130  
131 4 ▶sub serve {
Subroutine serve: called CC=3 SCAR=11.0
Pod undocumented
132  
133 4 my ($self)=@_;
134 4 my ($temporary_dn, $docs_dn, $config_fn)=$self->prepare();
135 4 $self->npm_install($temporary_dn);
136 4 my $host=$self->option('host', $MARKDOWN_PUBLISH_HOST);
137 4 my $port=$self->option('port', $MARKDOWN_PUBLISH_PORT);
138 4 ▶ $host='127.0.0.1' unless defined($host);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 2 2 4
139 4 ▶ $port=5173 unless defined($port);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 1 3 4
140 4 return $self->system_in_dir($temporary_dn, $self->option('npm', 'npm'),
141 'exec', '--', 'vitepress', 'dev', $docs_dn, '--config', $config_fn,
142 '--host', $host, '--port', $port);
143  
144 }
145 __END__
« Publish/Starlight.pm ↑ Summary Publish/Docusaurus.pm »