@forsakringskassan/docs-generator 2.11.4 → 2.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,10 +3,10 @@
3
3
  var path$1 = require('node:path');
4
4
  var esbuild = require('esbuild');
5
5
  var vue = require('vue');
6
- var vue3 = require('./vue3-CGUitvvB.js');
6
+ var vue3 = require('./vue3-rkJOanAd.js');
7
7
  var path = require('node:path/posix');
8
8
  require('@vue/compiler-sfc');
9
- require('./vendor-WsR_tA03.js');
9
+ require('./vendor-CJBsNZrh.js');
10
10
  require('node:url');
11
11
  require('fs');
12
12
  require('node:fs');
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var vendor = require('./vendor-WsR_tA03.js');
3
+ var vendor = require('./vendor-CJBsNZrh.js');
4
4
  var path = require('node:path/posix');
5
5
  var path$1 = require('node:path');
6
6
  require('crypto');
package/dist/index.d.ts CHANGED
@@ -77,7 +77,7 @@ declare interface Document_2 {
77
77
  /** Document outline (i.e. the heading structure) */
78
78
  outline: DocumentOutline;
79
79
  /** format of body */
80
- format: "markdown" | "html" | "json";
80
+ format: "markdown" | "html" | "json" | "redirect";
81
81
  tags: string[];
82
82
  template: string;
83
83
  fileInfo: FileInfo;
@@ -254,6 +254,13 @@ export declare interface GeneratorSiteOptions {
254
254
  lang?: string;
255
255
  }
256
256
 
257
+ /**
258
+ * Creates redirects using html-files with meta redirects.
259
+ *
260
+ * @public
261
+ */
262
+ export declare function htmlRedirectProcessor(): Processor;
263
+
257
264
  /**
258
265
  * @public
259
266
  */
@@ -266,6 +273,7 @@ export declare interface Manifest {
266
273
  pages: Array<{
267
274
  path: string;
268
275
  title: string;
276
+ redirect: null | string;
269
277
  outline: Array<{
270
278
  heading: string;
271
279
  anchor: string;
@@ -387,6 +395,7 @@ export declare interface NormalizedDocumentAttributes {
387
395
  href?: string;
388
396
  /** normalized sortorder (defaults to Infinity) */
389
397
  sortorder: number;
398
+ redirectFrom: string[];
390
399
  }
391
400
 
392
401
  /**
@@ -416,6 +425,7 @@ export declare interface ProcessorContext {
416
425
  readonly topnav: NavigationSection;
417
426
  readonly sidenav: NavigationSection;
418
427
  readonly resources: ResourceTask[];
428
+ readonly outputFolder: string;
419
429
  addDocument(document: Document_2 | Document_2[]): void;
420
430
  addVendorAsset(asset: VendorAsset | VendorAsset[]): void;
421
431
  /**
@@ -503,6 +513,15 @@ export declare interface ProcessorRuntime {
503
513
  */
504
514
  export declare type ProcessorStage = "generate-docs" | "generate-nav" | "assets" | "render";
505
515
 
516
+ /**
517
+ * Generate a netlify-style `_redirects` file.
518
+ *
519
+ * @public
520
+ * @param prefix - Prefix to append to all urls, set to subdirectory if you're deploying to a subdirectory.
521
+ * @param filename - Output filename (relative to output folder).
522
+ */
523
+ export declare function redirectFileProcessor(prefix?: string, filename?: string): Processor;
524
+
506
525
  /**
507
526
  * Represents a resource to be copied from `from` to `to`.
508
527
  *
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  var fs = require('node:fs/promises');
4
- var vendor = require('./vendor-WsR_tA03.js');
4
+ var vendor = require('./vendor-CJBsNZrh.js');
5
5
  var path = require('node:path');
6
6
  var require$$1 = require('crypto');
7
- var createMarkdownRenderer = require('./create-markdown-renderer-B1v_YUfy.js');
7
+ var createMarkdownRenderer = require('./create-markdown-renderer-D5z4jt-T.js');
8
8
  var node_child_process = require('node:child_process');
9
9
  var path$1 = require('node:path/posix');
10
10
  require('node:crypto');
@@ -17,7 +17,7 @@ var Module = require('node:module');
17
17
  var esbuild$1 = require('esbuild');
18
18
  var nunjucks = require('nunjucks');
19
19
  var vue = require('vue');
20
- var vue3 = require('./vue3-CGUitvvB.js');
20
+ var vue3 = require('./vue3-rkJOanAd.js');
21
21
  var express = require('express');
22
22
  require('fs');
23
23
  require('node:events');
@@ -311,15 +311,22 @@ function findExamples(doc) {
311
311
  return collected;
312
312
  }
313
313
  function manifestPageFromDocument(doc) {
314
- const { fileInfo } = doc;
314
+ const { body, fileInfo, format } = doc;
315
315
  return {
316
316
  path: createMarkdownRenderer.getOutputFilePath("", fileInfo),
317
317
  title: doc.attributes.title ?? doc.name ?? "",
318
+ redirect: format === "redirect" ? body : null,
318
319
  outline: flattenOutline(doc.outline),
319
320
  examples: findExamples(doc)
320
321
  };
321
322
  }
322
323
 
324
+ function line(page) {
325
+ if (page.redirect) {
326
+ return `${page.path} -> ${page.redirect}`;
327
+ }
328
+ return page.path;
329
+ }
323
330
  function renderMarkdown(manifest) {
324
331
  return [
325
332
  "## Documentation manifest",
@@ -327,7 +334,7 @@ function renderMarkdown(manifest) {
327
334
  "> Do not edit this file. It is a automatically generated by `@forsakringskassan/docs-generator`.",
328
335
  "",
329
336
  "```",
330
- ...manifest.pages.map((it) => it.path),
337
+ ...manifest.pages.map(line),
331
338
  "```",
332
339
  ""
333
340
  ].join("\n");
@@ -433,6 +440,157 @@ function themeSelectProcessor() {
433
440
  };
434
441
  }
435
442
 
443
+ function dump(value) {
444
+ return `<pre>${JSON.stringify(value, null, 2)}</pre>`;
445
+ }
446
+
447
+ function json(value) {
448
+ return JSON.stringify(value, null, 2);
449
+ }
450
+
451
+ function isExternalUrl(url) {
452
+ return url.startsWith("http://") || url.startsWith("https://") || url.startsWith("//");
453
+ }
454
+ function isAbsoluteUrl(url) {
455
+ return url.startsWith("/");
456
+ }
457
+ function relative(url, { fileInfo }) {
458
+ if (isExternalUrl(url)) {
459
+ return url;
460
+ }
461
+ if (isAbsoluteUrl(url)) {
462
+ url = `.${url}`;
463
+ }
464
+ url = createMarkdownRenderer.normalizePath(url);
465
+ const outputPath = createMarkdownRenderer.normalizePath(fileInfo.path);
466
+ if (outputPath === url || `${outputPath}/` === url) {
467
+ return "./";
468
+ }
469
+ const relative2 = path$1.relative(outputPath, url);
470
+ const prefix = relative2.startsWith(".") ? "" : "./";
471
+ const suffix = url.endsWith("/") ? "/" : "";
472
+ return [prefix, relative2, suffix].join("");
473
+ }
474
+
475
+ function take(haystack, key, value) {
476
+ return haystack.filter((it) => it[key] === value);
477
+ }
478
+
479
+ const template = (dst) => (
480
+ /* HTML */
481
+ `
482
+ <!doctype html>
483
+ <html lang="en">
484
+ <head>
485
+ <meta http-equiv="refresh" content="0; url=${dst}" />
486
+ </head>
487
+ <body>
488
+ <p>
489
+ This page has moved to
490
+ <a href="${dst}"> ${dst} </a>.
491
+ </p>
492
+ </body>
493
+ </html>
494
+ `
495
+ );
496
+ function htmlRedirectProcessor() {
497
+ return {
498
+ name: "html-redirect-processor",
499
+ after: "render",
500
+ async handler(context) {
501
+ const { docs, outputFolder } = context;
502
+ for (const doc of docs.filter(haveOutput)) {
503
+ if (doc.format !== "redirect") {
504
+ continue;
505
+ }
506
+ const filePath = createMarkdownRenderer.getOutputFilePath(outputFolder, doc.fileInfo);
507
+ const dst = relative(doc.body, doc);
508
+ const content = template(dst);
509
+ await fs.mkdir(path$1.dirname(filePath), { recursive: true });
510
+ await fs.writeFile(filePath, vendor.dedent(content), "utf-8");
511
+ }
512
+ }
513
+ };
514
+ }
515
+
516
+ function isRedirect(doc) {
517
+ return doc.format === "redirect";
518
+ }
519
+ function redirectFileProcessor(prefix = "/", filename = "_redirects") {
520
+ return {
521
+ name: "redirect-file-processor",
522
+ after: "render",
523
+ async handler(context) {
524
+ const { docs } = context;
525
+ const lines = docs.filter(haveOutput).filter(isRedirect).map((doc) => {
526
+ const src = createMarkdownRenderer.normalizePath(
527
+ doc.fileInfo.path,
528
+ doc.fileInfo.outputName
529
+ );
530
+ const dst = doc.body;
531
+ return `${prefix}${src} ${prefix}${dst} 301
532
+ `;
533
+ });
534
+ const filePath = path$1.join(context.outputFolder, filename);
535
+ await fs.mkdir(path$1.dirname(filePath), { recursive: true });
536
+ await fs.writeFile(filePath, lines.join(""), "utf-8");
537
+ context.log(filename, "generated");
538
+ }
539
+ };
540
+ }
541
+
542
+ function getRedirects(docs) {
543
+ return docs.map((it) => {
544
+ const { fileInfo } = it;
545
+ const { path, outputName } = fileInfo;
546
+ if (outputName === false) {
547
+ return [];
548
+ }
549
+ return it.attributes.redirectFrom.map((jt) => {
550
+ return {
551
+ from: jt.startsWith("/") ? jt.slice(1) : jt,
552
+ to: createMarkdownRenderer.normalizePath(path, outputName)
553
+ };
554
+ });
555
+ }).flat();
556
+ }
557
+
558
+ function redirectProcessor() {
559
+ return {
560
+ name: "redirect-processor",
561
+ after: "generate-docs",
562
+ async handler(context) {
563
+ const redirects = getRedirects(context.docs);
564
+ for (const redirect of redirects) {
565
+ const { dir, base, name } = path$1.parse(redirect.from);
566
+ context.addDocument({
567
+ id: `redirect:${redirect.from}-${redirect.to}`,
568
+ name: `${redirect.from}-${redirect.to}`,
569
+ alias: [],
570
+ visible: false,
571
+ attributes: {
572
+ sortorder: Infinity,
573
+ redirectFrom: []
574
+ },
575
+ body: redirect.to,
576
+ outline: [],
577
+ format: "redirect",
578
+ tags: [],
579
+ template: "redirect",
580
+ fileInfo: {
581
+ path: dir,
582
+ name,
583
+ fullPath: "virtual:redirect",
584
+ outputName: base
585
+ }
586
+ });
587
+ }
588
+ const s = redirects.length === 1 ? "" : "s";
589
+ context.log(redirects.length, `redirect${s} found`);
590
+ }
591
+ };
592
+ }
593
+
436
594
  function selectableVersionProcessor(pkg, container, options) {
437
595
  const { enabled = true, message = "Det finns en nyare version" } = options ?? {};
438
596
  return {
@@ -721,6 +879,7 @@ function parseFile$1(filePath, basePath, content) {
721
879
  status: attributes.status,
722
880
  badge: getBadge(attributes),
723
881
  component: getComponent(attributes),
882
+ redirectFrom: toArray$1(attributes.redirect_from ?? []),
724
883
  sortorder: attributes.sortorder ?? Infinity
725
884
  },
726
885
  body: blocks.body,
@@ -899,7 +1058,7 @@ function parseAPI(filePath, api) {
899
1058
  name: `vue:${componentName}`,
900
1059
  alias: [],
901
1060
  visible: false,
902
- attributes: { sortorder: Infinity },
1061
+ attributes: { sortorder: Infinity, redirectFrom: [] },
903
1062
  body: html,
904
1063
  outline: [],
905
1064
  format: "html",
@@ -980,7 +1139,8 @@ function parseFile(filePath, basePath, content) {
980
1139
  attributes: {
981
1140
  title,
982
1141
  href,
983
- sortorder: attributes.sortorder ?? Infinity
1142
+ sortorder: attributes.sortorder ?? Infinity,
1143
+ redirectFrom: []
984
1144
  },
985
1145
  body: content,
986
1146
  outline: [],
@@ -1451,6 +1611,9 @@ function generateNavtree(docs) {
1451
1611
  return parent;
1452
1612
  }
1453
1613
  for (const doc of docs) {
1614
+ if (doc.format === "redirect") {
1615
+ continue;
1616
+ }
1454
1617
  const [name, isSection] = pathFromDoc(doc);
1455
1618
  const title = doc.attributes.shortTitle ?? doc.attributes.title ?? doc.fileInfo.name;
1456
1619
  const sortorder = doc.attributes.sortorder;
@@ -1633,42 +1796,6 @@ Make sure the name is correct and the template file exists in one of the listed
1633
1796
  }
1634
1797
  }
1635
1798
 
1636
- function dump(value) {
1637
- return `<pre>${JSON.stringify(value, null, 2)}</pre>`;
1638
- }
1639
-
1640
- function json(value) {
1641
- return JSON.stringify(value, null, 2);
1642
- }
1643
-
1644
- function isExternalUrl(url) {
1645
- return url.startsWith("http://") || url.startsWith("https://") || url.startsWith("//");
1646
- }
1647
- function isAbsoluteUrl(url) {
1648
- return url.startsWith("/");
1649
- }
1650
- function relative(url, { fileInfo }) {
1651
- if (isExternalUrl(url)) {
1652
- return url;
1653
- }
1654
- if (isAbsoluteUrl(url)) {
1655
- url = `.${url}`;
1656
- }
1657
- url = createMarkdownRenderer.normalizePath(url);
1658
- const outputPath = createMarkdownRenderer.normalizePath(fileInfo.path);
1659
- if (outputPath === url || `${outputPath}/` === url) {
1660
- return "./";
1661
- }
1662
- const relative2 = path$1.relative(outputPath, url);
1663
- const prefix = relative2.startsWith(".") ? "" : "./";
1664
- const suffix = url.endsWith("/") ? "/" : "";
1665
- return [prefix, relative2, suffix].join("");
1666
- }
1667
-
1668
- function take(haystack, key, value) {
1669
- return haystack.filter((it) => it[key] === value);
1670
- }
1671
-
1672
1799
  class MissingTemplateError extends Error {
1673
1800
  searchDir;
1674
1801
  constructor(fileInfo, template, format, searchDir) {
@@ -1990,7 +2117,11 @@ function nunjucksProcessor(options) {
1990
2117
  }
1991
2118
  });
1992
2119
  }
2120
+ const formats = ["markdown", "json"];
1993
2121
  const docs = context.docs.filter((it) => {
2122
+ if (!formats.includes(it.format)) {
2123
+ return false;
2124
+ }
1994
2125
  return it.fileInfo.outputName;
1995
2126
  });
1996
2127
  progressbar.start(docs.length, 0, {
@@ -1998,7 +2129,7 @@ function nunjucksProcessor(options) {
1998
2129
  });
1999
2130
  const generatedFiles = [];
2000
2131
  try {
2001
- for (const doc of context.docs) {
2132
+ for (const doc of docs) {
2002
2133
  const filePath = await renderDocument(doc);
2003
2134
  if (filePath) {
2004
2135
  generatedFiles.push(filePath);
@@ -2296,7 +2427,7 @@ async function stage(stage2, context, processors, { verbose } = { verbose: true
2296
2427
  }
2297
2428
  return generatedFiles;
2298
2429
  }
2299
- function createContext(templateLoader) {
2430
+ function createContext(outputFolder, templateLoader) {
2300
2431
  let docs = [];
2301
2432
  let vendors = [];
2302
2433
  const resources = [];
@@ -2334,6 +2465,7 @@ function createContext(templateLoader) {
2334
2465
  get sidenav() {
2335
2466
  return sidenav;
2336
2467
  },
2468
+ outputFolder,
2337
2469
  addDocument(document) {
2338
2470
  docs = [...docs, ...toArray(document)];
2339
2471
  },
@@ -2464,10 +2596,11 @@ class Generator {
2464
2596
  async manifest(sourceFiles) {
2465
2597
  const processors = [
2466
2598
  fileReaderProcessor(sourceFiles),
2599
+ redirectProcessor(),
2467
2600
  ...this.processors
2468
2601
  ];
2469
2602
  const templateLoader = createTemplateLoader([]);
2470
- const context = createContext(templateLoader);
2603
+ const context = createContext("", templateLoader);
2471
2604
  await stage("generate-docs", context, processors, { verbose: false });
2472
2605
  const docs = context.docs.filter(haveOutput);
2473
2606
  const pages = docs.map(manifestPageFromDocument);
@@ -2488,6 +2621,7 @@ class Generator {
2488
2621
  await this._prepareFolders();
2489
2622
  const processors = [
2490
2623
  fileReaderProcessor(sourceFiles),
2624
+ redirectProcessor(),
2491
2625
  vendorProcessor(assetFolder, this.vendor),
2492
2626
  cssAssetProcessor(assetFolder, this.styles, { cwd }),
2493
2627
  jsAssetProcessor(assetFolder, this.scripts),
@@ -2509,7 +2643,7 @@ class Generator {
2509
2643
  ];
2510
2644
  compileProcessorRuntime(this, (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href)), processors);
2511
2645
  const templateLoader = createTemplateLoader(templateFolders);
2512
- const context = createContext(templateLoader);
2646
+ const context = createContext(outputFolder, templateLoader);
2513
2647
  const generatedFiles = [
2514
2648
  await stage("generate-docs", context, processors),
2515
2649
  await stage("generate-nav", context, processors),
@@ -2580,7 +2714,9 @@ function extractTerms(doc) {
2580
2714
  }
2581
2715
  }
2582
2716
  function isIndexable(doc) {
2583
- return Boolean(doc.visible && doc.fileInfo.outputName);
2717
+ return Boolean(
2718
+ doc.visible && doc.fileInfo.outputName && doc.format !== "redirect"
2719
+ );
2584
2720
  }
2585
2721
  function getTerms(doc) {
2586
2722
  return {
@@ -2609,7 +2745,8 @@ function searchProcessor() {
2609
2745
  alias: [],
2610
2746
  visible: false,
2611
2747
  attributes: {
2612
- sortorder: Infinity
2748
+ sortorder: Infinity,
2749
+ redirectFrom: []
2613
2750
  },
2614
2751
  body,
2615
2752
  outline: [],
@@ -2639,12 +2776,14 @@ exports.availableProcessors = availableProcessors;
2639
2776
  exports.cookieProcessor = cookieProcessor;
2640
2777
  exports.defineSources = defineSources;
2641
2778
  exports.frontMatterFileReader = frontMatterFileReader;
2779
+ exports.htmlRedirectProcessor = htmlRedirectProcessor;
2642
2780
  exports.livereloadProcessor = livereloadProcessor;
2643
2781
  exports.manifestProcessor = manifestProcessor;
2644
2782
  exports.matomoProcessor = matomoProcessor;
2645
2783
  exports.motdProcessor = motdProcessor;
2646
2784
  exports.navigationFileReader = navigationFileReader;
2647
2785
  exports.processorRuntimeName = processorRuntimeName;
2786
+ exports.redirectFileProcessor = redirectFileProcessor;
2648
2787
  exports.searchProcessor = searchProcessor;
2649
2788
  exports.selectableVersionProcessor = selectableVersionProcessor;
2650
2789
  exports.sourceUrlProcessor = sourceUrlProcessor;
@@ -27,7 +27,7 @@ declare interface Document_2 {
27
27
  /** Document outline (i.e. the heading structure) */
28
28
  outline: DocumentOutline;
29
29
  /** format of body */
30
- format: "markdown" | "html" | "json";
30
+ format: "markdown" | "html" | "json" | "redirect";
31
31
  tags: string[];
32
32
  template: string;
33
33
  fileInfo: FileInfo;
@@ -221,6 +221,7 @@ export declare interface NormalizedDocumentAttributes {
221
221
  href?: string;
222
222
  /** normalized sortorder (defaults to Infinity) */
223
223
  sortorder: number;
224
+ redirectFrom: string[];
224
225
  }
225
226
 
226
227
  /**
package/dist/markdown.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var createMarkdownRenderer = require('./create-markdown-renderer-B1v_YUfy.js');
4
- require('./vendor-WsR_tA03.js');
3
+ var createMarkdownRenderer = require('./create-markdown-renderer-D5z4jt-T.js');
4
+ require('./vendor-CJBsNZrh.js');
5
5
  require('node:url');
6
6
  require('node:path');
7
7
  require('fs');