blib/lib/Markdown/Publish/VitePress.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 118 CC 8 SCAR 27.4
CC
8
Cov
0%
prepare
42.0
serve
12.0
admonition_type
2.0
CRAP 15.5
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 use strict qw(vars);
19 use vars qw($VERSION @ISA);
20 use warnings;
21  
22  
23 # Parent and supporting packages
24 #
25 use Markdown::Publish ();
26 use Markdown::Publish::Constant;
27 use Cwd qw(abs_path);
28 use File::Path qw(make_path);
29 use File::Spec;
30 use JSON::PP qw(encode_json);
31  
32  
33 # Inheritance and version information
34 #
35 @ISA=qw(Markdown::Publish);
36 $VERSION='1.003';
37  
38  
39 # Done
40 #
41 1;
42  
43  
44 #======================================================================================================================
45  
46  
47 sub prepare {
48  
49 my ($self)=@_;
50 my ($temporary_dn, $docs_dn, $pages_ar)=$self->prepare_docs();
51 $self->promote_home($docs_dn, $pages_ar);
52 my $navigation_ar=$self->navigation($docs_dn, $pages_ar);
53 $self->normalize_node_markdown($docs_dn);
54 my $version=$self->option('version', 'latest');
55 my $package_hr={
56 type => 'module',
57 dependencies => {vitepress => $version}
58 };
59 $self->write_file(File::Spec->catfile($temporary_dn, 'package.json'),
60 encode_json($package_hr));
61 my $config_dn=File::Spec->catdir($docs_dn, '.vitepress');
62 make_path($config_dn);
63 my ($config_fn, $extend_fn)=$self->configuration_files();
64 my $prepared_config_fn;
65 if (defined($config_fn) && length($config_fn)) {
66 die "VitePress configuration not found: $config_fn\n" unless -f $config_fn;
67 $prepared_config_fn=abs_path($config_fn);
68 }
69 else {
70 my @items=map {
71 my $link=$_->{'id'} eq 'index' ? '/' : '/'.$_->{'id'};
72 " { text: ".encode_json($_->{'title'}).
73 ", link: ".encode_json($link)." }"
74 } @{$navigation_ar};
75 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 my $config;
81 if (defined($extend_fn)) {
82 die "VitePress configuration extension not found: $extend_fn\n"
83 unless -f $extend_fn;
84 my $context_hr=$self->configuration_context($pages_ar, $navigation_ar);
85 my $authored_fn=abs_path($extend_fn);
86 $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 $config="export default $generated;\n";
98 }
99 $prepared_config_fn=File::Spec->catfile($config_dn, 'config.mts');
100 $self->write_file($prepared_config_fn, $config);
101 }
102 return ($temporary_dn, $docs_dn, $prepared_config_fn);
103  
104 }
105  
106  
107 sub admonition_type {
108  
109 my ($self, $kind)=@_;
110 my %map=(note => 'info', tip => 'tip', warning => 'warning',
111 important => 'warning', caution => 'warning');
112 return $map{$kind} || $kind;
113  
114 }
115  
116  
117 sub build {
118  
119 my ($self)=@_;
120 my $output_dn=File::Spec->rel2abs($self->option('output', $MARKDOWN_PUBLISH_OUTPUT_DN));
121 my ($temporary_dn, $docs_dn, $config_fn)=$self->prepare();
122 $self->npm_install($temporary_dn);
123 $self->system_in_dir($temporary_dn, $self->option('npm', 'npm'),
124 'exec', '--', 'vitepress', 'build', $docs_dn, '--config', $config_fn,
125 '--outDir', $output_dn);
126 return $output_dn;
127  
128 }
129  
130  
131 sub serve {
132  
133 my ($self)=@_;
134 my ($temporary_dn, $docs_dn, $config_fn)=$self->prepare();
135 $self->npm_install($temporary_dn);
136 my $host=$self->option('host', $MARKDOWN_PUBLISH_HOST);
137 my $port=$self->option('port', $MARKDOWN_PUBLISH_PORT);
138 $host='127.0.0.1' unless defined($host);
139 $port=5173 unless defined($port);
140 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__