| 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 | 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 | my $site_dn=File::Spec->catdir($temporary_dn, 'docusaurus'); | ||
| 54 | my $site_docs_dn=File::Spec->catdir($site_dn, 'docs'); | ||
| 55 | make_path($site_docs_dn); | ||
| 56 | $self->copy_tree($docs_dn, $site_docs_dn); | ||
| 57 | $self->normalize_node_markdown($site_docs_dn); | ||
| 58 | my @items=map {{ | ||
| 59 | type => 'doc', | ||
| 60 | id => $_->{'id'}, | ||
| 61 | label => $_->{'title'} | ||
| 62 | }} @{$navigation_ar}; | ||
| 63 | my $version=$self->option('version', 'latest'); | ||
| 64 | 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 | $self->write_file(File::Spec->catfile($site_dn, 'package.json'), encode_json($package_hr)); | ||
| 75 | my ($config_fn, $extend_fn)=$self->configuration_files(); | ||
| 76 | my $prepared_config_fn; | ||
| 77 | if (defined($config_fn) && length($config_fn)) { | ||
| 78 | die "Docusaurus configuration not found: $config_fn\n" unless -f $config_fn; | ||
| 79 | $prepared_config_fn=abs_path($config_fn); | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | 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 | my $config; | ||
| 90 | if (defined($extend_fn)) { | ||
| 91 | die "Docusaurus configuration extension not found: $extend_fn\n" | ||
| 92 | unless -f $extend_fn; | ||
| 93 | my $context_hr=$self->configuration_context($pages_ar, $navigation_ar); | ||
| 94 | my $authored_fn=abs_path($extend_fn); | ||
| 95 | $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 | $config="module.exports = $generated;\n"; | ||
| 107 | } | ||
| 108 | $prepared_config_fn=File::Spec->catfile($site_dn, 'docusaurus.config.js'); | ||
| 109 | $self->write_file($prepared_config_fn, $config); | ||
| 110 | } | ||
| 111 | $self->write_file(File::Spec->catfile($site_dn, 'sidebars.js'), | ||
| 112 | "module.exports = { docs: ".encode_json(\@items)." };\n"); | ||
| 113 | return ($temporary_dn, $site_dn, $prepared_config_fn); | ||
| 114 | |||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | sub normalize_backend_markdown { | ||
| 119 | |||
| 120 | my ($self, $fn, $markdown)=@_; | ||
| 121 | return $self->title_frontmatter($fn, $markdown); | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub heading_anchor_required { | ||
| 127 | |||
| 128 | return 1; | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | sub admonition_type { | ||
| 134 | |||
| 135 | my ($self, $kind)=@_; | ||
| 136 | my %map=(note => 'note', tip => 'tip', warning => 'warning', | ||
| 137 | important => 'warning', caution => 'caution'); | ||
| 138 | return $map{$kind} || $kind; | ||
| 139 | |||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | sub build { | ||
| 144 | |||
| 145 | my ($self)=@_; | ||
| 146 | my $output_dn=File::Spec->rel2abs($self->option('output', $MARKDOWN_PUBLISH_OUTPUT_DN)); | ||
| 147 | my (undef, $site_dn, $config_fn)=$self->prepare(); | ||
| 148 | $self->npm_install($site_dn); | ||
| 149 | $self->system_in_dir($site_dn, $self->option('npm', 'npm'), | ||
| 150 | 'run', 'build', '--', '--config', $config_fn, '--out-dir', $output_dn); | ||
| 151 | return $output_dn; | ||
| 152 | |||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | sub serve { | ||
| 157 | |||
| 158 | my ($self)=@_; | ||
| 159 | my (undef, $site_dn, $config_fn)=$self->prepare(); | ||
| 160 | $self->npm_install($site_dn); | ||
| 161 | my $host=$self->option('host', $MARKDOWN_PUBLISH_HOST); | ||
| 162 | my $port=$self->option('port', $MARKDOWN_PUBLISH_PORT); | ||
| 163 | $host='127.0.0.1' unless defined($host); | ||
| 164 | $port=3001 unless defined($port); | ||
| 165 | 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__ |