@dosgato/templating 0.0.19 → 0.0.22

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.
@@ -1,5 +1,5 @@
1
- import { PageWithAncestors, ComponentData } from './component';
2
- import { LinkDefinition } from './links';
1
+ import { PageWithAncestors, ComponentData } from './component.js';
2
+ import { LinkDefinition } from './links.js';
3
3
  export declare type APITemplateType = 'page' | 'component' | 'data';
4
4
  /**
5
5
  * This interface lays out the structure the API needs for each template in the system.
@@ -90,3 +90,15 @@ export interface Migration {
90
90
  }
91
91
  export declare type LinkGatheringFn = (data: any) => LinkDefinition[];
92
92
  export declare type FulltextGatheringFn = (data: any) => string[];
93
+ /**
94
+ * This function is used by API template definitions to help them identify links inside large blocks
95
+ * of text and return them for indexing.
96
+ */
97
+ export declare function extractLinksFromText(text: string): LinkDefinition[];
98
+ /**
99
+ * This function is used by API template definitions to help them identify all the searchable
100
+ * words in a large block of text and return them for indexing.
101
+ */
102
+ export declare function getKeywords(text: string, options?: {
103
+ stopwords?: boolean;
104
+ }): string[];
@@ -1,2 +1,21 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ import { stopwords } from './stopwords.js';
2
+ /**
3
+ * This function is used by API template definitions to help them identify links inside large blocks
4
+ * of text and return them for indexing.
5
+ */
6
+ export function extractLinksFromText(text) {
7
+ const matches = text.matchAll(/{.*"type"\s?:\s+"\w+".*?}/gi);
8
+ return Array.from(matches).map(m => JSON.parse(m[0]));
9
+ }
10
+ /**
11
+ * This function is used by API template definitions to help them identify all the searchable
12
+ * words in a large block of text and return them for indexing.
13
+ */
14
+ export function getKeywords(text, options) {
15
+ return Array.from(new Set(text
16
+ .toLocaleLowerCase()
17
+ .normalize('NFD').replace(/\p{Diacritic}/gu, '')
18
+ .split(/[^\w-]+/)
19
+ .flatMap(word => word.includes('-') ? word.split('-').concat(word.replace('-', '')) : [word])
20
+ .filter(word => word.length > 2 && (options?.stopwords === false || !stopwords[word]) && isNaN(Number(word)))));
21
+ }
@@ -1,5 +1,6 @@
1
- import { EditBarOpts } from './editbar';
2
- import { ResourceProvider } from './provider';
1
+ import { EditBarOpts } from './editbar.js';
2
+ import { LinkDefinition } from './links.js';
3
+ import { ResourceProvider } from './provider.js';
3
4
  /**
4
5
  * This is the primary templating class to build your templates. Subclass it and provide
5
6
  * at least a render function.
@@ -18,6 +19,19 @@ export declare abstract class Component<DataType extends ComponentData = any, Fe
18
19
  parent?: Component;
19
20
  page?: Page;
20
21
  hadError: boolean;
22
+ /**
23
+ * This function will be provided by the rendering server and should be used inside your fetch,
24
+ * method to convert a link, as input by a user, into a URL suitable for an href, or optionally
25
+ * an absolute URL suitable for a backend http request or non-HTML document like an RSS feed.
26
+ */
27
+ resolveLink: (link: string | LinkDefinition, absolute?: boolean) => Promise<string>;
28
+ /**
29
+ * This function will be provided by the rendering server and should be used inside your fetch
30
+ * method to prepare editor-provided HTML for rendering. It will do things like find and resolve
31
+ * link definitions in the internal dosgato format and clean up tags that were accidentally left
32
+ * open to protect overall page integrity.
33
+ */
34
+ processRich: (text: string) => Promise<string>;
21
35
  /**
22
36
  * The first phase of rendering a component is the fetch phase. Each component may
23
37
  * provide a fetch method that looks up data it needs from external sources. This step
@@ -31,14 +45,14 @@ export declare abstract class Component<DataType extends ComponentData = any, Fe
31
45
  */
32
46
  fetch(editMode: boolean): Promise<FetchedType>;
33
47
  /**
34
- * The second phase of rendering a component is the context phase. This step is TOP-DOWN,
35
- * each component will receive the parent component's context, modify it as desired,
36
- * and then pass context to its children.
48
+ * The second phase of rendering a component is the context phase. This step is TOP-DOWN and
49
+ * NON-MUTATING. Each component will receive the parent component's context and then pass a
50
+ * NEW context object to its children.
37
51
  *
38
52
  * This is useful for rendering logic that is sensitive to where the component exists in
39
53
  * the hierarchy of the page. For instance, if a parent component has used an h2 header
40
54
  * already, it will want to inform its children so that they can use h3 next, and they inform
41
- * their children that h4 is next, and so on. (Header level tracking is actually required in
55
+ * their children that h4 is next, and so on. (Header level tracking is supported by default in
42
56
  * dosgato CMS.)
43
57
  *
44
58
  * This function may return a promise in case you need to do something asynchronous based on
package/dist/component.js CHANGED
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Page = exports.Component = void 0;
4
- const editbar_1 = require("./editbar");
5
- const provider_1 = require("./provider");
1
+ import { editBar, newBar } from './editbar.js';
2
+ import { ResourceProvider } from './provider.js';
6
3
  /**
7
4
  * This is the primary templating class to build your templates. Subclass it and provide
8
5
  * at least a render function.
@@ -10,7 +7,7 @@ const provider_1 = require("./provider");
10
7
  * During rendering, it will be "hydrated" - placed into a full page structure with its
11
8
  * parent and child components linked.
12
9
  */
13
- class Component extends provider_1.ResourceProvider {
10
+ export class Component extends ResourceProvider {
14
11
  // the constructor is part of the recursive hydration mechanism: constructing
15
12
  // a Component will also construct/hydrate all its child components
16
13
  constructor(data, path, parent) {
@@ -45,14 +42,14 @@ class Component extends provider_1.ResourceProvider {
45
42
  return undefined;
46
43
  }
47
44
  /**
48
- * The second phase of rendering a component is the context phase. This step is TOP-DOWN,
49
- * each component will receive the parent component's context, modify it as desired,
50
- * and then pass context to its children.
45
+ * The second phase of rendering a component is the context phase. This step is TOP-DOWN and
46
+ * NON-MUTATING. Each component will receive the parent component's context and then pass a
47
+ * NEW context object to its children.
51
48
  *
52
49
  * This is useful for rendering logic that is sensitive to where the component exists in
53
50
  * the hierarchy of the page. For instance, if a parent component has used an h2 header
54
51
  * already, it will want to inform its children so that they can use h3 next, and they inform
55
- * their children that h4 is next, and so on. (Header level tracking is actually required in
52
+ * their children that h4 is next, and so on. (Header level tracking is supported by default in
56
53
  * dosgato CMS.)
57
54
  *
58
55
  * This function may return a promise in case you need to do something asynchronous based on
@@ -150,7 +147,7 @@ class Component extends provider_1.ResourceProvider {
150
147
  editBar(opts = {}) {
151
148
  opts.label ?? (opts.label = this.editLabel());
152
149
  opts.extraClass ?? (opts.extraClass = this.editClass());
153
- return (0, editbar_1.editBar)(this.path, opts);
150
+ return editBar(this.path, opts);
154
151
  }
155
152
  /**
156
153
  * Components may override this function to provide a custom new bar
@@ -160,11 +157,10 @@ class Component extends provider_1.ResourceProvider {
160
157
  newBar(areaName, opts = {}) {
161
158
  opts.label ?? (opts.label = this.newLabel(areaName));
162
159
  opts.extraClass ?? (opts.extraClass = this.newClass(areaName));
163
- return (0, editbar_1.newBar)(this.path + '.' + areaName, opts);
160
+ return newBar(this.path + '.' + areaName, opts);
164
161
  }
165
162
  }
166
- exports.Component = Component;
167
- class Page extends Component {
163
+ export class Page extends Component {
168
164
  constructor(page) {
169
165
  super(page.data, '/', undefined);
170
166
  this.pagePath = page.path;
@@ -174,4 +170,3 @@ class Page extends Component {
174
170
  console.warn(`Recoverable issue occured during render of ${this.pagePath}. Component at ${path} threw the following error:`, e);
175
171
  }
176
172
  }
177
- exports.Page = Page;
package/dist/editbar.js CHANGED
@@ -1,28 +1,23 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.newBar = exports.editBar = void 0;
4
- const txstate_utils_1 = require("txstate-utils");
5
- function editBar(path, opts) {
1
+ import { htmlEncode, randomid } from 'txstate-utils';
2
+ export function editBar(path, opts) {
6
3
  if (!opts.editMode)
7
4
  return '';
8
- const id = (0, txstate_utils_1.randomid)();
5
+ const id = randomid();
9
6
  return `
10
- <div class="dg-edit-bar ${opts.extraClass ?? ''}" data-path="${(0, txstate_utils_1.htmlEncode)(path)}" draggable="true" ondragstart="window.dgEditing.drag(event)" ondragover="window.dgEditing.over(event)" ondragend="window.dgEditing.drop(event)">
11
- <span id="${id}" class="dg-edit-bar-label">${(0, txstate_utils_1.htmlEncode)(opts.label)}</span>
7
+ <div class="dg-edit-bar ${opts.extraClass ?? ''}" data-path="${htmlEncode(path)}" draggable="true" ondragstart="window.dgEditing.drag(event)" ondragover="window.dgEditing.over(event)" ondragend="window.dgEditing.drop(event)">
8
+ <span id="${id}" class="dg-edit-bar-label">${htmlEncode(opts.label)}</span>
12
9
  <button onclick="window.dgEditing.edit(event)" aria-describedby="${id}">Edit</button>
13
10
  <button onclick="window.dgEditing.move(event)" aria-describedby="${id}">Move</button>
14
11
  <button onclick="window.dgEditing.del(event)" aria-describedby="${id}">Trash</button>
15
12
  </div>
16
13
  `.trim();
17
14
  }
18
- exports.editBar = editBar;
19
- function newBar(path, opts) {
15
+ export function newBar(path, opts) {
20
16
  if (!opts.editMode)
21
17
  return '';
22
18
  return `
23
- <div role="button" onclick="window.dgEditing.create(event)" class="dg-new-bar ${opts.extraClass ?? ''}" data-path="${(0, txstate_utils_1.htmlEncode)(path)}">
24
- ${(0, txstate_utils_1.htmlEncode)(opts.label)}
19
+ <div role="button" onclick="window.dgEditing.create(event)" class="dg-new-bar ${opts.extraClass ?? ''}" data-path="${htmlEncode(path)}">
20
+ ${htmlEncode(opts.label)}
25
21
  </div>
26
22
  `.trim();
27
23
  }
28
- exports.newBar = newBar;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- export * from './apitemplate';
2
- export * from './component';
3
- export * from './links';
4
- export * from './provider';
5
- export * from './uitemplate';
1
+ export * from './apitemplate.js';
2
+ export * from './component.js';
3
+ export * from './links.js';
4
+ export * from './provider.js';
5
+ export * from './render.js';
6
+ export * from './stopwords.js';
7
+ export * from './uitemplate.js';
package/dist/index.js CHANGED
@@ -1,21 +1,7 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./apitemplate"), exports);
18
- __exportStar(require("./component"), exports);
19
- __exportStar(require("./links"), exports);
20
- __exportStar(require("./provider"), exports);
21
- __exportStar(require("./uitemplate"), exports);
1
+ export * from './apitemplate.js';
2
+ export * from './component.js';
3
+ export * from './links.js';
4
+ export * from './provider.js';
5
+ export * from './render.js';
6
+ export * from './stopwords.js';
7
+ export * from './uitemplate.js';
package/dist/links.d.ts CHANGED
@@ -63,15 +63,3 @@ export interface DataFolderLink {
63
63
  path: string;
64
64
  }
65
65
  export declare type LinkDefinition = AssetLink | AssetFolderLink | PageLink | WebLink | DataLink | DataFolderLink;
66
- /**
67
- * This function is used by API template definitions to help them identify links inside large blocks
68
- * of text and return them for indexing.
69
- */
70
- export declare function extractLinksFromText(text: string): LinkDefinition[];
71
- /**
72
- * This function is used by API template definitions to help them identify all the searchable
73
- * words in a large block of text and return them for indexing.
74
- */
75
- export declare function getKeywords(text: string, options?: {
76
- stopwords?: boolean;
77
- }): string[];
package/dist/links.js CHANGED
@@ -1,26 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getKeywords = exports.extractLinksFromText = void 0;
4
- const stopwords_1 = require("./stopwords");
5
- /**
6
- * This function is used by API template definitions to help them identify links inside large blocks
7
- * of text and return them for indexing.
8
- */
9
- function extractLinksFromText(text) {
10
- const matches = text.matchAll(/{.*"type"\s?:\s+"\w+".*?}/gi);
11
- return Array.from(matches).map(m => JSON.parse(m[0]));
12
- }
13
- exports.extractLinksFromText = extractLinksFromText;
14
- /**
15
- * This function is used by API template definitions to help them identify all the searchable
16
- * words in a large block of text and return them for indexing.
17
- */
18
- function getKeywords(text, options) {
19
- return Array.from(new Set(text
20
- .toLocaleLowerCase()
21
- .normalize('NFD').replace(/\p{Diacritic}/gu, '')
22
- .split(/[^\w-]+/)
23
- .flatMap(word => word.includes('-') ? word.split('-').concat(word.replace('-', '')) : [word])
24
- .filter(word => word.length > 2 && (options?.stopwords === false || !stopwords_1.stopwords[word]) && isNaN(Number(word)))));
25
- }
26
- exports.getKeywords = getKeywords;
1
+ export {};
package/dist/provider.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
1
  /* eslint-disable @typescript-eslint/no-extraneous-class */
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.ResourceProvider = void 0;
5
2
  /**
6
3
  * This class is a parent class for Component, but it can also be used as a standalone
7
4
  * if you are creating a set of templates with shared resources. This will be fairly
@@ -11,10 +8,9 @@ exports.ResourceProvider = void 0;
11
8
  *
12
9
  * If you do this, don't forget to register the provider along with your templates!
13
10
  */
14
- class ResourceProvider {
11
+ export class ResourceProvider {
15
12
  static webpath(name) { return this.webpaths.get(name); }
16
13
  }
17
- exports.ResourceProvider = ResourceProvider;
18
14
  /**
19
15
  * Each template should provide a map of CSS blocks where the map key is the unique name for
20
16
  * the CSS and the value is the CSS itself. For instance, if a template needs CSS from a
@@ -0,0 +1,5 @@
1
+ import { ContextBase } from './component.js';
2
+ export declare function printHeader(ctx: ContextBase, content: string): string;
3
+ export declare function advanceHeader(ctx: ContextBase, content?: string): {
4
+ headerLevel: number;
5
+ };
package/dist/render.js ADDED
@@ -0,0 +1,17 @@
1
+ import { isBlank } from 'txstate-utils';
2
+ export function printHeader(ctx, content) {
3
+ if (isBlank(content))
4
+ return '';
5
+ const level = (ctx.headerLevel ?? 0) + 1;
6
+ if (level < 1)
7
+ return `<h1>${content}</h1>`;
8
+ if (level > 6)
9
+ return `<h6>${content}</h1>`;
10
+ return `<h${level}>${content}</h${level}>`;
11
+ }
12
+ export function advanceHeader(ctx, content) {
13
+ const ret = { ...ctx };
14
+ if (!isBlank(content))
15
+ ret.headerLevel = (ret.headerLevel ?? 0) + 1;
16
+ return ret;
17
+ }
package/dist/stopwords.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stopwords = void 0;
4
- exports.stopwords = {
1
+ export const stopwords = {
5
2
  myself: true,
6
3
  our: true,
7
4
  ours: true,
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@dosgato/templating",
3
- "version": "0.0.19",
3
+ "version": "0.0.22",
4
4
  "description": "A library to support building templates for dosgato CMS.",
5
+ "type": "module",
5
6
  "exports": {
6
- "require": "./dist/index.js",
7
- "import": "./dist-esm/index.js"
7
+ ".": "./dist/index.js",
8
+ "./package.json": "./package.json"
8
9
  },
9
10
  "types": "dist/index.d.ts",
10
11
  "scripts": {
@@ -36,6 +37,6 @@
36
37
  },
37
38
  "homepage": "https://github.com/txstate-etc/dosgato-templating#readme",
38
39
  "files": [
39
- "dist", "dist-esm"
40
+ "dist"
40
41
  ]
41
42
  }
package/dist-esm/index.js DELETED
@@ -1,7 +0,0 @@
1
- import all from '../dist/index.js'
2
-
3
- export const ResourceProvider = all.ResourceProvider
4
- export const Component = all.Component
5
- export const Page = all.Page
6
- export const extractLinksFromText = all.extractLinksFromText
7
- export const getKeywords = all.getKeywords
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }