Publish/Starlight.pm

filter: Statementstmt 97.6% 126 / 129 Branchbran 84.3% 27 / 32 Conditioncond 73.9% 17 / 23 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 8 totaltotal 87.4% 195 / 223 CC 26 SCAR 20.8
CC
26
Cov
92%
starlight_title_heading
16.0
prepare
6.0
admonition_type
6.0
CRAP 8.0
« Publish.pm ↑ Summary Publish/VitePress.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::Starlight;
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 4 ▶sub prepare {
Subroutine prepare: called CC=6 SCAR=17.9
Pod undocumented
48  
49 4 my ($self)=@_;
50 4 my ($temporary_dn, $docs_dn, $pages_ar)=$self->prepare_docs();
51 4 $self->promote_home($docs_dn, $pages_ar);
52 4 my $navigation_ar=$self->navigation($docs_dn, $pages_ar);
53 4 my $site_dn=File::Spec->catdir($temporary_dn, 'starlight');
54 4 my $site_docs_dn=File::Spec->catdir($site_dn, 'src', 'content', 'docs');
55 4 make_path($site_docs_dn);
56 4 $self->copy_tree($docs_dn, $site_docs_dn);
57 4 $self->normalize_node_markdown($site_docs_dn);
58 4 my $astro_version=$self->option('astro_version', 'latest');
59 4 my $starlight_version=$self->option('starlight_version', 'latest');
60 4 my $package_hr={
61 type => 'module',
62 scripts => {build => 'astro build', start => 'astro dev'},
63 dependencies => {
64 astro => $astro_version,
65 '@astrojs/starlight' => $starlight_version,
66 '@astrojs/markdown-remark' => 'latest',
67 'github-slugger' => 'latest'
68 }
69 };
70 4 $self->write_file(File::Spec->catfile($site_dn, 'package.json'), encode_json($package_hr));
71 4 $self->write_file(File::Spec->catfile($site_dn, 'local-links.mjs'), $self->local_links_plugin());
72 4 my ($config_fn, $extend_fn)=$self->configuration_files();
73 4 my $prepared_config_fn;
74 4 ▶ if (defined($config_fn) && length($config_fn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_fn and length $config_fn) { } 1 3 4
Condition66%2/3
defined $config_fn and length $config_fn
!ll&&!rl&&r
301
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
75 1 ▶ die "Starlight configuration not found: $config_fn\n" unless -f $config_fn;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $config_fn 0 1 1
76 1 my $authored_fn=abs_path($config_fn);
77 1 my $config="import { defineConfig, mergeConfig } from 'astro/config';\n".
78 "import { pathToFileURL } from 'node:url';\n".
79 "import { unified } from '\@astrojs/markdown-remark';\n".
80 "import localLinks from './local-links.mjs';\n\n".
81 "const authored = (await import(pathToFileURL(".encode_json($authored_fn).
82 ").href)).default;\n".
83 "const markdown = authored.markdown || {};\n".
84 "const processor = markdown.processor;\n".
85 "if (processor && processor.name !== 'unified') throw new Error('Starlight link resolution requires unified Markdown processing');\n".
86 "const options = processor ? processor.options : {};\n".
87 "const configured = unified({ ...options, remarkPlugins: [...(options.remarkPlugins || []), ...(markdown.remarkPlugins || []), localLinks] });\n".
88 "export default defineConfig(mergeConfig(authored, { markdown: { processor: configured } }));\n";
89 1 $prepared_config_fn=File::Spec->catfile($site_dn, 'astro.config.mjs');
90 1 $self->write_file($prepared_config_fn, $config);
91 }
92 else {
93 my @items=map {
94 # Astro normalizes content collection slugs to lower case.
95 15 my $slug=lc($_->{'id'});
96 15 " { label: ".encode_json($_->{'title'}).
97 ", slug: ".encode_json($slug)." }"
98 3 } @{$navigation_ar};
99 3 my $imports="import { defineConfig } from 'astro/config';\n".
100 "import { unified } from '\@astrojs/markdown-remark';\n".
101 "import starlight from '\@astrojs/starlight';\n\n".
102 "import localLinks from './local-links.mjs';\n";
103 3 my $astro="{\n base: ".encode_json($self->site_base('/')).",\n".
104 " markdown: { processor: unified({ remarkPlugins: [localLinks] }) }\n}";
105 3 my $starlight="{\n title: ".
106 encode_json($self->option('name', 'Documentation')).
107 ",\n sidebar: [{ label: 'Docs', items: [\n".
108 join(",\n", @items)."\n ] }]\n }";
109 3 my $config;
110 3 ▶ if (defined($extend_fn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extend_fn) { } 1 2 3
111 1 ▶ die "Starlight configuration extension not found: $extend_fn\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $extend_fn 0 1 1
112 unless -f $extend_fn;
113 1 my $context_hr=$self->configuration_context($pages_ar, $navigation_ar);
114 1 my $authored_fn=abs_path($extend_fn);
115 1 $config=$imports."import { pathToFileURL } from 'node:url';\n\n".
116 "const generated = { astro: $astro, starlight: $starlight };\n".
117 "const context = ".encode_json($context_hr).";\n".
118 "const extend = (await import(pathToFileURL(".
119 encode_json($authored_fn).").href)).default;\n".
120 "if (typeof extend !== 'function') throw new Error('Starlight config_extend must export a default function');\n".
121 "const configured = await extend(generated, context);\n".
122 "if (!configured || typeof configured !== 'object' || !configured.astro || !configured.starlight) throw new Error('Starlight config_extend must return astro and starlight configuration objects');\n".
123 "const integrations = configured.astro.integrations || [];\n".
124 "if (!Array.isArray(integrations)) throw new Error('Starlight config_extend astro.integrations must be an array');\n".
125 "const { integrations: unused, ...astro } = configured.astro;\n".
126 "export default defineConfig({ ...astro, integrations: [starlight(configured.starlight), ...integrations] });\n";
127 }
128 else {
129 2 $config=$imports."\nexport default defineConfig({ ...$astro, integrations: [starlight($starlight)] });\n";
130 }
131 3 $prepared_config_fn=File::Spec->catfile($site_dn, 'astro.config.mjs');
132 3 $self->write_file($prepared_config_fn, $config);
133 }
134 4 $self->write_file(File::Spec->catfile($site_dn, 'src', 'content.config.ts'),
135 "import { defineCollection } from 'astro:content';\n".
136 "import { glob } from 'astro/loaders';\n".
137 "import { docsSchema } from '\@astrojs/starlight/schema';\n\n".
138 "export const collections = { docs: defineCollection({ loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/docs' }), schema: docsSchema() }) };\n");
139 4 return ($temporary_dn, $site_dn, $prepared_config_fn);
140  
141 }
142  
143  
144 4 ▶sub local_links_plugin {
Subroutine local_links_plugin: called CC=1 SCAR=0.0
Pod undocumented
145  
146 # Astro routes Markdown through slugged page IDs; resolve authored file
147 # links while their source paths are still available to remark.
148 #
149 4 return <<'JAVASCRIPT';
150 import { existsSync } from 'node:fs';
151 import path from 'node:path';
152 import { slug } from 'github-slugger';
153  
154 const root = path.resolve('src/content/docs');
155  
156 function route(filename) {
157 const parts = path.relative(root, filename).replace(/\\/g, '/').replace(/\.mdx?$/i, '').split('/');
158 const id = parts.map((part) => slug(part)).join('/');
159 if (id === 'index') return '/';
160 if (parts[parts.length - 1].toLowerCase() === 'index') return '/' + parts.slice(0, -1).map((part) => slug(part)).join('/') + '/';
161 return '/' + id + '/';
162 }
163  
164 export default function localLinks() {
165 return (tree, file) => {
166 function visit(node) {
167 if ((node.type === 'link' || node.type === 'definition') && typeof node.url === 'string') {
168 const match = /^([^?#]+\.mdx?)(\?[^#]*)?(#.*)?$/i.exec(node.url);
169 if (match && !match[1].startsWith('/') && !match[1].includes('://')) {
170 const target = path.resolve(path.dirname(file.path), decodeURI(match[1]));
171 if (target.startsWith(root + path.sep) && existsSync(target)) {
172 const from = route(file.path);
173 const to = route(target);
174 node.url = (path.posix.relative(from, to) || '.') + '/' + (match[2] || '') + (match[3] || '');
175 }
176 }
177 }
178 for (const child of node.children || []) visit(child);
179 }
180 visit(tree);
181 };
182 }
183 JAVASCRIPT
184  
185 }
186  
187  
188 36 ▶sub normalize_backend_markdown {
Subroutine normalize_backend_markdown: called CC=1 SCAR=0.0
Pod undocumented
189  
190 36 my ($self, $fn, $markdown)=@_;
191 36 $markdown=$self->title_frontmatter($fn, $markdown);
192 36 return $self->starlight_title_heading($markdown);
193  
194 }
195  
196  
197 328 ▶sub heading_anchor_required {
Subroutine heading_anchor_required: called CC=1 SCAR=0.0
Pod undocumented
198  
199 328 return 1;
200  
201 }
202  
203  
204 0 ▶sub admonition_type {
Subroutine admonition_type: not called CC=2 SCAR=17.9
Pod undocumented
205  
206 0 my ($self, $kind)=@_;
207 0 my %map=(note => 'note', tip => 'tip', warning => 'caution',
208 important => 'caution', caution => 'caution');
209 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
210  
211 }
212  
213  
214 36 ▶sub starlight_title_heading {
Subroutine starlight_title_heading: called CC=16 SCAR=27.7
Pod undocumented
215  
216 36 my ($self, $markdown)=@_;
217 36 my @output;
218 36 my ($fence, $length, $frontmatter, $removed)=('', 0, 0, 0);
219 36 my $index=0;
220 36 foreach my $line (split(/(?<=\n)/, $markdown)) {
221 360 ▶ if (!$index && $line=~/^---[ \t]*\r?\n?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $index and $line =~ /^---[ \t]*\r?\n?$/) 36 324 360
Condition66%2/3
not $index and $line =~ /^---[ \t]*\r?\n?$/
!ll&&!rl&&r
324036
MC/DC50%1/2
not $index and $line =~ /^---[ \t]*\r?\n?$/
not $index $line =~ /^---[ \t]*\r?\n?$/
Truth table66%2/3
not(A) and B
A= not $indexB= $line =~ /^---[ \t]*\r?\n?$/
ABresult
0X0
100
111
222 36 $frontmatter=1;
223 36 push(@output, $line);
224 36 $index++;
225 36 next;
226 }
227 324 ▶ if ($frontmatter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frontmatter) 72 252 324
228 72 ▶ $frontmatter=0 if $line=~/^---[ \t]*\r?\n?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^---[ \t]*\r?\n?$/ 36 36 72
229 72 push(@output, $line);
230 72 $index++;
231 72 next;
232 }
233 252 ▶ if (!$fence && $line=~/^ {0,3}(`{3,}|~{3,})/) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $fence and $line =~ /^ {0,3}(`{3,}|~{3,})/) { } 8 244 252
elsif ($fence and $line =~ /^ {0,3}\Q$fence\E{$length,}\s*$/) { } 8 236 244
elsif (not $fence || $removed and $line =~ /^#\s+(.+?)[ \t]*(\r?\n)?$/) { } 36 364 400
Condition100%3/3
not $fence and $line =~ /^ {0,3}(`{3,}|~{3,})/
!ll&&!rl&&r
242208
MC/DC100%2/2
not $fence and $line =~ /^ {0,3}(`{3,}|~{3,})/
not $fence $line =~ /^ {0,3}(`{3,}|~{3,})/
Truth table100%3/3
not(A) and B
A= not $fenceB= $line =~ /^ {0,3}(`{3,}|~{3,})/
ABresult
0X0
100
111
234 8 $fence=substr($1, 0, 1);
235 8 $length=length($1);
236 }
237 ▶ elsif ($fence && $line=~/^ {0,3}\Q$fence\E{$length,}\s*$/) {
Condition100%3/3
$fence and $line =~ /^ {0,3}\Q$fence\E{$length,}\s*$/
!ll&&!rl&&r
220168
MC/DC100%2/2
$fence and $line =~ /^ {0,3}\Q$fence\E{$length,}\s*$/
$fence $line =~ /^ {0,3}\Q$fence\E{$length,}\s*$/
Truth table100%3/3
A and B
A= $fenceB= $line =~ /^ {0,3}\Q$fence\E{$length,}\s*$/
ABresult
0X0
100
111
238 8 $fence='';
239 }
240 ▶ elsif (!$fence && !$removed && $line=~/^#\s+(.+?)[ \t]*(\r?\n)?$/) {
Condition100%6/6
not $fence || $removed and $line =~ /^#\s+(.+?)[ \t]*(\r?\n)?$/
!ll&&!rl&&r
1643636
$fence || $removed
l!l&&r!l&&!r
1614872
MC/DC100%3/3
not $fence || $removed and $line =~ /^#\s+(.+?)[ \t]*(\r?\n)?$/
$fence $removed $line =~ /^#\s+(.+?)[ \t]*(\r?\n)?$/
Truth table100%4/4
not(A || B) and C
A= $fenceB= $removedC= $line =~ /^#\s+(.+?)[ \t]*(\r?\n)?$/
ABCresult
1XX0
01X0
0000
0011
241 36 ▶ my ($title, $newline)=($1, $2 || '');
Condition50%1/2
$2 || ''
l!l
360
MC/DC0%0/1
$2 || ''
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
242 36 my ($id)=$title=~/\s+\{#([\w.-]+)(?:\s+[^}]*)?\}\s*$/;
243 36 ▶ unless (defined($id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 20 16 36
244 20 $id=lc($title);
245 20 $id=~s/[^a-z0-9]+/-/g;
246 20 $id=~s/^-|-$//g;
247 }
248 36 ▶ push(@output, "<a id=\"$id\"></a>$newline") if length($id);
Branch: 1 branch
BranchTrueFalseTotal
if length $id 36 0 36
249 36 $removed=1;
250 36 $index++;
251 36 next;
252 }
253 216 push(@output, $line);
254 216 $index++;
255 }
256 36 return join('', @output);
257  
258 }
259  
260  
261 1 ▶sub build {
Subroutine build: called CC=2 SCAR=7.0
Pod undocumented
262  
263 1 my ($self)=@_;
264 1 my $output_dn=File::Spec->rel2abs($self->option('output', $MARKDOWN_PUBLISH_OUTPUT_DN));
265 1 my (undef, $site_dn, $config_fn)=$self->prepare();
266 1 my $config_arg=File::Spec->abs2rel($config_fn, $site_dn);
267 1 ▶ $config_arg=$config_fn if $config_arg=~m{^\.\.[/\\]};
Branch: 1 branch
BranchTrueFalseTotal
if $config_arg =~ m(^\.\.[/\\]) 0 1 1
268 1 $self->npm_install($site_dn);
269 1 $self->system_in_dir($site_dn, $self->option('npm', 'npm'),
270 'run', 'build', '--', '--config', $config_arg, '--outDir', $output_dn);
271 1 return $output_dn;
272  
273 }
274  
275  
276 4 ▶sub serve {
Subroutine serve: called CC=4 SCAR=13.9
Pod undocumented
277  
278 4 my ($self)=@_;
279 4 my (undef, $site_dn, $config_fn)=$self->prepare();
280 4 my $config_arg=File::Spec->abs2rel($config_fn, $site_dn);
281 4 ▶ $config_arg=$config_fn if $config_arg=~m{^\.\.[/\\]};
Branch: 1 branch
BranchTrueFalseTotal
if $config_arg =~ m(^\.\.[/\\]) 4 0 4
282 4 $self->npm_install($site_dn);
283 4 local $ENV{'ASTRO_DEV_BACKGROUND'}=0;
284 4 my $host=$self->option('host', $MARKDOWN_PUBLISH_HOST);
285 4 my $port=$self->option('port', $MARKDOWN_PUBLISH_PORT);
286 4 ▶ $host='127.0.0.1' unless defined($host);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 2 2 4
287 4 ▶ $port=4321 unless defined($port);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 1 3 4
288 4 return $self->system_in_dir($site_dn, $self->option('npm', 'npm'),
289 'run', 'start', '--', '--config', $config_arg,
290 '--host', $host, '--port', $port);
291  
292 }
293 __END__
« Publish.pm ↑ Summary Publish/VitePress.pm »