@pterodoc/wordpress 0.2.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.
package/src/url.ts ADDED
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Where pages live on WordPress.
3
+ *
4
+ * URL policy belongs to the target: the renderer asks for a path and gets one
5
+ * back, without knowing whether the site nests pages, uses a subdirectory
6
+ * install, or publishes versions under their own segment.
7
+ */
8
+
9
+ import { joinPath, segments, slugify } from '@pterodoc/core/util';
10
+
11
+ /** How a WordPress site's documentation tree is addressed. */
12
+ export interface WordpressUrlPolicy {
13
+ /** Segments of the path the tree hangs from, e.g. ['docstack']. */
14
+ rootSegments: string[];
15
+ /** Segments below the root holding the docs, e.g. ['docs']. May be empty. */
16
+ baseSegments: string[];
17
+ /** Name of the version published at the base, which needs no segment of its own. */
18
+ primaryVersion?: string | undefined;
19
+ /** Locale published at the base; other locales get a segment. */
20
+ primaryLocale?: string | undefined;
21
+ }
22
+
23
+ /**
24
+ * Segments that come before a page's own path.
25
+ *
26
+ * A version or a locale only earns a segment when it is not the primary one,
27
+ * so a single-version, single-locale site publishes exactly where it did
28
+ * before any of this existed.
29
+ */
30
+ export function prefixSegments(
31
+ policy: WordpressUrlPolicy,
32
+ context: { versionName: string; locale: string },
33
+ ): string[] {
34
+ const parts = [...policy.rootSegments, ...policy.baseSegments];
35
+ if (policy.primaryLocale !== undefined && context.locale !== policy.primaryLocale) {
36
+ parts.push(slugify(context.locale));
37
+ }
38
+ if (policy.primaryVersion !== undefined && context.versionName !== policy.primaryVersion) {
39
+ parts.push(slugify(context.versionName));
40
+ }
41
+ return parts;
42
+ }
43
+
44
+ /** The absolute site path of a page. */
45
+ export function hrefFor(
46
+ policy: WordpressUrlPolicy,
47
+ treePath: string,
48
+ context: { versionName: string; locale: string },
49
+ ): string {
50
+ return joinPath([...prefixSegments(policy, context), ...segments(treePath)]);
51
+ }
52
+
53
+ /**
54
+ * The path pages hang from, and the slug of the page that owns the tree.
55
+ *
56
+ * Everything above the owned page is created once if missing and never edited;
57
+ * the owned page is the documentation root itself.
58
+ */
59
+ export function splitOwnership(policy: WordpressUrlPolicy): {
60
+ stubSegments: string[];
61
+ rootSlug: string;
62
+ } {
63
+ const all = [...policy.rootSegments, ...policy.baseSegments];
64
+ const rootSlug = all[all.length - 1];
65
+ if (rootSlug === undefined) {
66
+ throw new Error('There is nowhere to publish: the root path and the base are both empty.');
67
+ }
68
+ return { stubSegments: all.slice(0, -1), rootSlug };
69
+ }