signalk-template-views 0.0.0-stage → 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/CHANGELOG.md ADDED
@@ -0,0 +1,36 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0
4
+
5
+ - **Behaviour change:** `usesDescription` is true only when the view
6
+ **prints** `@description` (`{{@description}}`, `{{{@description}}}`,
7
+ inside a section or not), following the revised §1.7. A section that only
8
+ tests it (`{{#@description}}`, `{{^@description}}`, or a comma list
9
+ containing it) no longer counts, so the host shows the description
10
+ itself. A comparison (`{{@description|eq:x}}`) prints `Yes` or `No`, not
11
+ the description, so it does not count either.
12
+ - The escaping of a leading `#` run (`## Note`) and of a value made only of
13
+ hyphens (`--`) is now in the specification; their test cases are no
14
+ longer marked `package`. Output is unchanged.
15
+ - Docs: §1.8 now names `,` among the characters a field name cannot
16
+ contain.
17
+
18
+ ## 0.1.0
19
+
20
+ First release: the reference renderer and validator for resource-template
21
+ views (Signal K Structured Resources, §1.7).
22
+
23
+ - `renderView`, `renderText`: Mustache view text rendered to Markdown, with
24
+ Markdown escaping, `|` format specifiers (`<unit>`, `value`, `raw`), the
25
+ `eq` comparison, "any of several" comma-list sections
26
+ (`{{#a,b}}…{{/a,b}}`), `@` members, and the value and section rules.
27
+ - `renderSummary`, `summaryLine`, `displayFields`: the one-line summary with
28
+ its fallback to the template's `summary` field list, and the field values a
29
+ host's own detail display needs.
30
+ - `usesDescription`: whether a view places `@description`, from its tags.
31
+ - `validateViews`, `validateViewText`: the template-provider checks (§1.9),
32
+ including formats in section tags and misplaced comma lists, with
33
+ warnings for tags naming undeclared fields.
34
+ - `PROPERTY_NAME_PATTERN`, `isValidPropertyName`, `isReservedName`: the
35
+ property-name (§1.8) and reserved-name (§1.6) rules.
36
+ - Language-neutral JSON test cases in `cases/`.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Joel Kozikowski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,133 @@
1
- # Temporary Holding Version
1
+ # signalk-template-views
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The reference renderer for Signal K **resource-template views**.
4
+
5
+ A Signal K resource template (part of the *Structured Resources* proposal,
6
+ [SignalK/signalk-server#3407](https://github.com/SignalK/signalk-server/issues/3407))
7
+ can carry **views**: small [Mustache](https://mustache.github.io/) templates
8
+ that say how to display one of its resources. A marina template might say:
9
+
10
+ ```jsonc
11
+ "views": {
12
+ "detail": {
13
+ "text": "{{{@description}}}\n\n| | |\n|---|---|\n{{#depth}}| Depth | {{depth}} |\n{{/depth}}{{#fuel}}| Fuel | {{fuel}} |\n{{/fuel}}"
14
+ },
15
+ "summary": { "text": "{{kind}}{{#depth}} · {{depth}}{{/depth}}{{#fuel}} · fuel{{/fuel}}" }
16
+ }
17
+ ```
18
+
19
+ This package turns a view and a resource into Markdown, following the
20
+ specification's rules: unit-aware values (`{{depth}}` → `8 ft`), format
21
+ specifiers (`{{depth|m}}`, `{{depth|value}}`, `{{depth|raw}}`), conditions
22
+ (`{{#kind|eq:full}}…{{/kind|eq:full}}`, and "any of several":
23
+ `{{#fuel,water}}…{{/fuel,water}}`), `@` members such as `@name` and
24
+ `@position`, and Markdown escaping. It also validates views for template
25
+ providers, and ships language-neutral test cases.
26
+
27
+ It is used by chart plotters such as Freeboard-SK and by template editors. It
28
+ is a plain library: it is not a Signal K server plugin or web app.
29
+
30
+ ## Install
31
+
32
+ ```sh
33
+ npm install signalk-template-views
34
+ ```
35
+
36
+ ESM and CommonJS. Its only dependency is `mustache`.
37
+
38
+ ## Render a view
39
+
40
+ ```ts
41
+ import { renderView, renderSummary, usesDescription } from 'signalk-template-views'
42
+
43
+ const result = renderView(template, resource, 'detail', { formatters })
44
+ if (result.ok) {
45
+ showMarkdown(result.markdown)
46
+ // Show the resource's description yourself unless the view prints it.
47
+ if (!usesDescription(template.views?.detail)) showDescription(resource.description)
48
+ } else {
49
+ showMyOwnFieldDisplay() // result.reason says why: absent, unsupported-format, invalid, error
50
+ }
51
+
52
+ // One line for a list row: the summary view, or the template's `summary` fields.
53
+ const { markdown } = renderSummary(template, resource, { formatters })
54
+ ```
55
+
56
+ The result is GitHub-Flavored Markdown. Render it with your Markdown renderer;
57
+ a summary may be reduced to plain text and truncated.
58
+
59
+ **Security.** Rendered output is untrusted, and the host sanitizes it
60
+ before display (§1.7). A view's text comes from whoever published the
61
+ template, and `{{{x}}}` inserts values unchanged, so the Markdown can
62
+ contain raw HTML and links with any URL scheme. Only `{{x}}` values are
63
+ escaped, and even an escaped value can supply a link's URL
64
+ (`[site]({{website}})`). Render it with raw HTML disabled or sanitized, and
65
+ filter link and image URLs (for example, drop `javascript:`), as you would
66
+ for any user-supplied Markdown.
67
+
68
+ ## Formatters: your units, your locale
69
+
70
+ The package never fetches anything. Number, date-time and position formatting
71
+ belong to the host application, which knows the user's Unit Preferences and
72
+ locale. Pass them as `formatters`; each is optional:
73
+
74
+ ```ts
75
+ const formatters = {
76
+ // value is stored in the base unit of request.category (e.g. metres for depth).
77
+ // request.unit is set for {{depth|ft}}; request.symbol is false for {{depth|value}}.
78
+ // Return null for a unit you can't convert to: the renderer then shows the bare value.
79
+ number: (value, request) => formatInPreferredUnits(value, request),
80
+ dateTime: (iso, request) => new Date(iso).toLocaleString(),
81
+ position: ({ latitude, longitude }, part) => formatPosition(latitude, longitude, part)
82
+ }
83
+ ```
84
+
85
+ Without them, numbers show as plain decimals with no unit, date-times as
86
+ stored, and positions in decimal degrees.
87
+
88
+ A view may nest sections at most 64 deep (`MAX_SECTION_DEPTH`); deeper text
89
+ is invalid.
90
+
91
+ ## Validate views (template providers and editors)
92
+
93
+ ```ts
94
+ import { validateViews, isValidPropertyName } from 'signalk-template-views'
95
+
96
+ const { valid, errors, warnings } = validateViews(template)
97
+ // errors: reasons to reject the template (bad shape, unsupported format,
98
+ // invalid Mustache, partials, delimiter changes, a format in a section tag,
99
+ // a comma list outside a section tag or with a specifier)
100
+ // warnings: tags that name fields the schema does not declare, and more
101
+ ```
102
+
103
+ `isValidPropertyName(name)` applies the specification's property-name rule
104
+ (`^[A-Za-z_][A-Za-z0-9_:-]*$`).
105
+
106
+ ## API
107
+
108
+ | Export | Purpose |
109
+ |---|---|
110
+ | `renderView(template, resource, viewName, options?)` | Render a template's view; `{ ok, markdown }` or `{ ok: false, reason }` |
111
+ | `renderText(text, template, resource, options?)` | Render view text directly (live preview in an editor) |
112
+ | `renderSummary(template, resource, options?)` | The summary view, or the `summary` field-list fallback |
113
+ | `summaryLine(template, resource, options?)` | The `summary` field-list line alone |
114
+ | `displayFields(template, resource, options?)` | Fields that display a value, titled, for a host's own detail display |
115
+ | `usesDescription(view)` | Whether the view prints `@description` (a section that only tests it does not count) |
116
+ | `validateViews(template)`, `validateViewText(text, template?)` | Template-provider validation |
117
+ | `escapeMarkdown(value)` | The Markdown escaping `{{x}}` applies |
118
+ | `isValidPropertyName`, `PROPERTY_NAME_PATTERN`, `isReservedName` | Name rules |
119
+ | `parseTag`, `resourceBag`, `declaredFields`, `pointPosition`, `getView` | Lower-level helpers |
120
+
121
+ `options` is `{ formatters?, bag? }`; `bag` overrides where the resource's
122
+ fields are read from.
123
+
124
+ ## Test cases for other implementations
125
+
126
+ The `cases/` folder (also in the npm package) holds JSON test cases: template,
127
+ resource, view, formatter answers, and the expected Markdown. A client written
128
+ in another language can run them to check itself. See
129
+ [`cases/README.md`](cases/README.md).
130
+
131
+ ## License
132
+
133
+ MIT
@@ -0,0 +1,132 @@
1
+ # Test cases for resource-template views
2
+
3
+ Language-neutral test cases for the **Views** section of the Signal K
4
+ Structured Resources specification (resource templates, §1.7). Any client
5
+ that renders views, in any language, can run them to check its
6
+ implementation.
7
+
8
+ They ship in the npm package (`signalk-template-views/cases/*.json`) and live
9
+ in the repository's `cases/` folder. They are plain JSON: load them, build the
10
+ formatter stubs described below, and compare.
11
+
12
+ ## Files
13
+
14
+ | File | What it checks |
15
+ |---|---|
16
+ | `fixtures.json` | Shared templates, resources and formatter answers that the other files refer to by name |
17
+ | `render.json` | Rendering a view or view text to Markdown, and the cases where a view fails |
18
+ | `summary.json` | The one-line summary: the `summary` view, or the fallback `summary` field list |
19
+ | `escape.json` | Markdown escaping of a `{{x}}` value |
20
+ | `validate.json` | Validating a template's `views` the way a template provider must (§1.9) |
21
+ | `uses-description.json` | Whether a view prints `@description` (a section that only tests it does not count) |
22
+
23
+ Every file has a `description` and a `cases` array.
24
+
25
+ ## What is normative
26
+
27
+ Most cases check rules the specification states. Cases marked
28
+ `"package": true` check this reference implementation's choices where the
29
+ specification is silent or leaves room (they are listed as **[package]** in
30
+ the repository's `REQUIREMENTS.md`). An implementation that chooses
31
+ differently may skip them.
32
+
33
+ Two whole areas need care:
34
+
35
+ - **Escaping.** The specification defines a minimum set and lets a client
36
+ escape further ASCII punctuation. The unmarked cases assume exactly the
37
+ minimum set. An implementation that escapes more should compare after
38
+ removing backslash escapes from both sides, or skip them.
39
+ - **The summary fallback.** The specification says a client falls back to
40
+ the `summary` field list; the line's format (` · ` separator, a true
41
+ boolean shown as its title) is this package's, so the `fields` cases in
42
+ `summary.json` are marked `package`. Their `source` is still normative:
43
+ when the fallback is used is the specification's rule, so an
44
+ implementation with its own line format should still check `source`.
45
+
46
+ ## Fixtures and references
47
+
48
+ In `render.json` and `summary.json`, a case's `template` and `resource` are
49
+ either an object, or a string naming an entry in `fixtures.json`'s
50
+ `templates` or `resources`.
51
+
52
+ ## Formatter stubs
53
+
54
+ The specification leaves number, date-time and position formatting to the
55
+ client (its Unit Preferences, locale and position format). The cases replace
56
+ them with stubs that return fixed answers from a `formatted` object. A case's
57
+ own `formatted` is used when present (even an empty `{}`, which makes every
58
+ stub return nothing); otherwise `fixtures.json`'s `formatted` is used.
59
+
60
+ - **Numbers** (`formatted.numbers`), looked up by a key:
61
+ - `<field>` for the display value in the user's unit, with its symbol
62
+ (`{{depth}}`);
63
+ - `<field>|value` for the display number without a symbol
64
+ (`{{depth|value}}`);
65
+ - `<field>|<unit>` for a conversion to that unit (`{{depth|fathom}}`).
66
+
67
+ A missing key means the formatter cannot produce it. For a unit, the
68
+ renderer then shows the bare display value (an unknown unit). For a bare or
69
+ `value` request, it shows the number's plain decimal form, such as `16` or
70
+ `2.4384`.
71
+ - **Date-times** (`formatted.dateTimes`), looked up by the stored string. A
72
+ missing key means the stored string is shown.
73
+ - **Positions** (`formatted.position`), with keys `position`, `latitude` and
74
+ `longitude`, for `@position`, `@latitude` and `@longitude`. A missing key
75
+ means the default: decimal degrees, and `<latitude>, <longitude>` for
76
+ `@position`.
77
+
78
+ The plain decimal form and the position default are this reference
79
+ implementation's fallbacks, not the specification's. The cases that rely on
80
+ them use values whose decimal form is the same in any language.
81
+
82
+ ## Render cases
83
+
84
+ A case has `text` (view text rendered directly against the template), or
85
+ `view` (the name of a view to look up in the template's `views`). It expects
86
+ either `expected`, the exact Markdown, or `failure`:
87
+
88
+ - `absent`: the template has no view by that name;
89
+ - `unsupported-format`: the view's `format` is not `text/markdown`;
90
+ - `invalid`: the view's `text` is not a string, is not valid Mustache, nests
91
+ sections more than 64 deep (this package's limit), uses a partial or a
92
+ delimiter change, puts a format in a section tag, or uses a comma list
93
+ outside a section tag or with a specifier. (The specification has a
94
+ template provider reject the last two; failing the render is this
95
+ package's choice, so those cases are marked `package`.)
96
+
97
+ On any failure a client uses its fallback (§1.7).
98
+
99
+ ## Validation cases
100
+
101
+ `valid` is whether the template's `views` pass the view checks of §1.9 (the
102
+ fixture template is not otherwise a valid template: it declares a reserved
103
+ name on purpose). `errors` lists
104
+ the error codes in the order found; `warnings` lists each warning's `code` and
105
+ `tag` (as written), once per distinct tag in each view, in tag order; for one
106
+ tag, `unknown-member` or `undeclared-field` comes first, then
107
+ `unknown-operator`, then `member-format`. When a text has more than one
108
+ parse error, `invalid-mustache` wins when the text does not parse as
109
+ Mustache at all; otherwise the first in the text is reported. A comma list
110
+ gives one reference warning per bad item, all with the list's tag. The
111
+ codes are this package's names:
112
+
113
+ | Code | Kind | Meaning |
114
+ |---|---|---|
115
+ | `views-not-object` | error | `views` is not an object |
116
+ | `view-not-object` | error | a view is not an object |
117
+ | `text-not-string` | error | a view has no string `text` |
118
+ | `unsupported-format` | error | a view's `format` is present and not `text/markdown` |
119
+ | `invalid-mustache` | error | `text` does not parse as Mustache |
120
+ | `nesting-too-deep` | error | sections nest more than 64 deep (this package's limit) |
121
+ | `partial-not-allowed` | error | `text` uses a partial |
122
+ | `delimiter-change-not-allowed` | error | `text` changes the delimiters |
123
+ | `format-in-section` | error | a section tag carries a format (`{{#fuel\|raw}}`) |
124
+ | `list-not-in-section` | error | a comma list in a plain tag (`{{a,b}}`) |
125
+ | `list-with-specifier` | error | a comma list with a specifier (`{{#a,b\|eq:x}}`) |
126
+ | `undeclared-field` | warning | a tag names a field the schema does not declare |
127
+ | `unknown-member` | warning | a tag names an `@` member the spec does not define |
128
+ | `unknown-operator` | warning | a tag uses an operator the spec does not define |
129
+ | `member-format` | warning | a tag puts a format on an `@` member; none are defined, so it prints the plain value |
130
+
131
+ Another implementation may use its own codes; map them, or compare only
132
+ `valid` and the warning tags.
@@ -0,0 +1,259 @@
1
+ {
2
+ "description": "Markdown escaping of a {{x}} value: `value` escapes to `escaped`. Cases marked `package` go beyond the spec's minimum set, which the spec permits: a block marker after up to three leading spaces, and hyphens followed only by spaces or tabs treated as a value of only hyphens.",
3
+ "cases": [
4
+ {
5
+ "value": "plain text",
6
+ "escaped": "plain text"
7
+ },
8
+ {
9
+ "value": "a\\b`c*d_e[f]g<h>i|j~k&l",
10
+ "escaped": "a\\\\b\\`c\\*d\\_e\\[f\\]g\\<h\\>i\\|j\\~k\\&l"
11
+ },
12
+ {
13
+ "value": "# heading",
14
+ "escaped": "\\# heading"
15
+ },
16
+ {
17
+ "value": "+ item",
18
+ "escaped": "\\+ item"
19
+ },
20
+ {
21
+ "value": "- item",
22
+ "escaped": "\\- item"
23
+ },
24
+ {
25
+ "value": "= rule",
26
+ "escaped": "\\= rule"
27
+ },
28
+ {
29
+ "value": "> quote",
30
+ "escaped": "\\> quote"
31
+ },
32
+ {
33
+ "value": "1. first",
34
+ "escaped": "1\\. first"
35
+ },
36
+ {
37
+ "value": "12) twelfth",
38
+ "escaped": "12\\) twelfth"
39
+ },
40
+ {
41
+ "value": "2.5",
42
+ "escaped": "2.5"
43
+ },
44
+ {
45
+ "value": "100",
46
+ "escaped": "100"
47
+ },
48
+ {
49
+ "value": "#1",
50
+ "escaped": "#1"
51
+ },
52
+ {
53
+ "value": "-",
54
+ "escaped": "\\-"
55
+ },
56
+ {
57
+ "value": "- milk",
58
+ "escaped": "\\- milk"
59
+ },
60
+ {
61
+ "value": "1. milk",
62
+ "escaped": "1\\. milk"
63
+ },
64
+ {
65
+ "value": "-81.1",
66
+ "escaped": "-81.1"
67
+ },
68
+ {
69
+ "value": "2.4 m",
70
+ "escaped": "2.4 m"
71
+ },
72
+ {
73
+ "value": "#5",
74
+ "escaped": "#5"
75
+ },
76
+ {
77
+ "value": "#",
78
+ "escaped": "\\#"
79
+ },
80
+ {
81
+ "value": "+",
82
+ "escaped": "\\+"
83
+ },
84
+ {
85
+ "value": "1.",
86
+ "escaped": "1\\."
87
+ },
88
+ {
89
+ "value": "3)",
90
+ "escaped": "3\\)"
91
+ },
92
+ {
93
+ "value": "1)x",
94
+ "escaped": "1)x"
95
+ },
96
+ {
97
+ "value": "+1 555 0100",
98
+ "escaped": "+1 555 0100"
99
+ },
100
+ {
101
+ "value": "-\tx",
102
+ "escaped": "\\-\tx"
103
+ },
104
+ {
105
+ "value": "#\tx",
106
+ "escaped": "\\#\tx"
107
+ },
108
+ {
109
+ "value": "1.\tx",
110
+ "escaped": "1\\.\tx"
111
+ },
112
+ {
113
+ "value": "=5",
114
+ "escaped": "\\=5"
115
+ },
116
+ {
117
+ "value": ">5",
118
+ "escaped": "\\>5"
119
+ },
120
+ {
121
+ "value": "-- x",
122
+ "escaped": "-- x"
123
+ },
124
+ {
125
+ "value": "- - -",
126
+ "escaped": "\\- - -"
127
+ },
128
+ {
129
+ "value": "-\nitem",
130
+ "escaped": "\\- item"
131
+ },
132
+ {
133
+ "value": "## Note",
134
+ "escaped": "\\## Note"
135
+ },
136
+ {
137
+ "value": "## heading",
138
+ "escaped": "\\## heading"
139
+ },
140
+ {
141
+ "value": "###",
142
+ "escaped": "\\###"
143
+ },
144
+ {
145
+ "value": "##5",
146
+ "escaped": "##5"
147
+ },
148
+ {
149
+ "value": "##\tx",
150
+ "escaped": "\\##\tx"
151
+ },
152
+ {
153
+ "value": "--",
154
+ "escaped": "\\--"
155
+ },
156
+ {
157
+ "value": "---",
158
+ "escaped": "\\---"
159
+ },
160
+ {
161
+ "value": "--x",
162
+ "escaped": "--x"
163
+ },
164
+ {
165
+ "value": "## ",
166
+ "escaped": "\\## "
167
+ },
168
+ {
169
+ "value": "a. b",
170
+ "escaped": "a. b"
171
+ },
172
+ {
173
+ "value": "x # y - z + w = v",
174
+ "escaped": "x # y - z + w = v"
175
+ },
176
+ {
177
+ "value": "3 + 4 = 7",
178
+ "escaped": "3 + 4 = 7"
179
+ },
180
+ {
181
+ "value": "line1\nline2",
182
+ "escaped": "line1 line2"
183
+ },
184
+ {
185
+ "value": "a\r\nb\rc",
186
+ "escaped": "a b c"
187
+ },
188
+ {
189
+ "value": "café, Ünïcode °′″",
190
+ "escaped": "café, Ünïcode °′″"
191
+ },
192
+ {
193
+ "value": "braces {x} (y) !z .w",
194
+ "escaped": "braces {x} (y) !z .w"
195
+ },
196
+ {
197
+ "value": " > quote",
198
+ "escaped": " \\> quote"
199
+ },
200
+ {
201
+ "value": "\t- tabbed",
202
+ "escaped": "\t- tabbed"
203
+ },
204
+ {
205
+ "value": " - code",
206
+ "escaped": " - code"
207
+ },
208
+ {
209
+ "value": " # indented",
210
+ "escaped": " \\# indented",
211
+ "package": true
212
+ },
213
+ {
214
+ "value": " 1. indented",
215
+ "escaped": " 1\\. indented",
216
+ "package": true
217
+ },
218
+ {
219
+ "value": "\n# after a break",
220
+ "escaped": " \\# after a break",
221
+ "package": true
222
+ },
223
+ {
224
+ "value": " + item",
225
+ "escaped": " \\+ item",
226
+ "package": true
227
+ },
228
+ {
229
+ "value": " -5",
230
+ "escaped": " -5",
231
+ "package": true
232
+ },
233
+ {
234
+ "value": " 2.4 m",
235
+ "escaped": " 2.4 m",
236
+ "package": true
237
+ },
238
+ {
239
+ "value": " ## x",
240
+ "escaped": " \\## x",
241
+ "package": true
242
+ },
243
+ {
244
+ "value": " --",
245
+ "escaped": " \\--",
246
+ "package": true
247
+ },
248
+ {
249
+ "value": "--- ",
250
+ "escaped": "\\--- ",
251
+ "package": true
252
+ },
253
+ {
254
+ "value": "-- \t",
255
+ "escaped": "\\-- \t",
256
+ "package": true
257
+ }
258
+ ]
259
+ }