- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
//
// This file is a TypeScript type definition and validation function for the JSONCanvas 1.0.
// <https://jsoncanvas.org/spec/1.0/>
export type Red = "1";
export type Orange = "2";
export type Yellow = "3";
export type Green = "4";
export type Cyan = "5";
export type Purple = "6";
// TypeScript can't handle strict representation due to union limit, but
// that does not benefit so much. Also, the JSONCanvas "spec" does not define
// what hex format is (alpha? shorthand? uppercase?), so this is okay.
type HexColorFormat = `#${string}`;
const HEX_COLOR_FORMAT_REGEXP_PATTERN = /^#[a-z0-9]{3}([a-z0-9]{3})?$/i;
export function isHexColorFormat(x: unknown): x is HexColorFormat {
if (typeof x !== "string") {
return false;
}
return HEX_COLOR_FORMAT_REGEXP_PATTERN.test(x);
}
export type CanvasColor =
| Red
| Orange
| Yellow
| Green
| Cyan
| Purple
| HexColorFormat;
export function isCanvasColor(x: unknown): x is CanvasColor {
switch (x) {
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
return true;
default:
return isHexColorFormat(x);
}
}
export interface GenericNode {
/**
* A unique ID for the node.
* NOTE: Length range and available characters are not defined in the spec.
*/
id: string;
/**
* The node type.
*/
type: string;
/**
* The x position of the node in pixels.
* NOTE: The spec does not define value range. Safe to use largest signed interger.
* NOTE: This type currently does not care bigint ranges.
*/
x: number;
/**
* The y position of the node in pixels.
* NOTE: The spec does not define value range. Safe to use largest signed interger.
* NOTE: This type currently does not care bigint ranges.
*/
y: number;
/**
* The width of the node in pixels
* NOTE: The spec does not defined value range.
* Safe to use largest unsigned integer, with minimum value guard on parsing.
*/
width: number;
/**
* The height of the node in pixels
* NOTE: The spec does not defined value range.
* Safe to use largest unsigned integer, with minimum value guard on parsing.
*/
height: number;
/**
* The color of the node.
*/
color?: CanvasColor;
}
export function isGenericNode(x: unknown): x is GenericNode {
if (typeof x !== "object" || !x) {
return false;
}
if (!("id" in x) || typeof x.id !== "string") {
return false;
}
if (!("type" in x) || typeof x.type !== "string") {
return false;
}
if (!("x" in x) || typeof x.x !== "number" || !Number.isFinite(x.x)) {
return false;
}
if (!("y" in x) || typeof x.y !== "number" || !Number.isFinite(x.y)) {
return false;
}
if (
!("width" in x) || typeof x.width !== "number" || !Number.isFinite(x.width)
) {
return false;
}
if (
!("height" in x) || typeof x.height !== "number" ||
!Number.isFinite(x.height)
) {
return false;
}
if ("color" in x && !isCanvasColor(x.color)) {
return false;
}
return true;
}
/**
* You can change `Markdown` type parameter to other types.
* For example, AST if it needs to be displayed as HTML.
*/
export interface TextNode<Markdown = string> extends GenericNode {
type: "text";
/**
* Plain text with Markdown syntax.
* NOTE: Although the spec does not define what "Markdown" is, Obsidian seems to
* using/parsing their Obsidian Flavored Markdown. Parsing and displaying
* of this property is UB.
*/
text: Markdown;
}
export function isTextNode(x: GenericNode): x is TextNode {
if (x.type !== "text") {
return false;
}
if (!("text" in x) || typeof x.text !== "string") {
return false;
}
return true;
}
export interface FileNode extends GenericNode {
type: "file";
/**
* The path to the file within the system.
* NOTE: Syntax (e.g. what separator to use?) and resolution is UB.
*/
file: string;
/**
* A subpath that may link to a heading or a block. Always starts with a #.
* NOTE: Both "a heading" and "a block" are not defined in the spec, UB.
*/
subpath?: `#${string}`;
}
export function isFileNode(x: GenericNode): x is FileNode {
if (x.type !== "file") {
return false;
}
if (!("file" in x) || typeof x.file !== "string") {
return false;
}
if (
"subpath" in x &&
!(typeof x.subpath === "string" && x.subpath.startsWith("#"))
) {
return false;
}
return true;
}
export interface LinkNode extends GenericNode {
type: "link";
/**
* NOTE: Format is not defined in the spec.
*/
url: string;
}
export function isLinkNode(x: GenericNode): x is LinkNode {
if (x.type !== "link") {
return false;
}
if (!("url" in x && typeof x.url === "string")) {
return false;
}
return true;
}
export interface GroupNode extends GenericNode {
type: "group";
/**
* A text label for the group.
* NOTE: Length range and available characters are not defined in the spec.
*/
label?: string;
/**
* The path to the background image.
* NOTE: Syntax (e.g. what separator to use?) and resolution is UB.
*/
background?: string;
/**
* the rendering style of the background image. Valid values:
* - `cover` ... fills the entire width and height of the node.
* - `ratio` ... maintains the aspect ratio of the background image.
* - `repeat` ... repeats the image as a pattern in both x/y directions.
* NOTE: UB when the field is empty.
*/
backgroundStyle?: "cover" | "ratio" | "repeat";
}
export function isGroupNode(x: GenericNode): x is GroupNode {
if (x.type !== "group") {
return false;
}
if ("label" in x && typeof x.label !== "string") {
return false;
}
if ("background" in x && typeof x.background !== "string") {
return false;
}
if ("backgroundStyle" in x) {
switch (x.backgroundStyle) {
case "cover":
case "ratio":
case "repeat":
break;
default:
return false;
}
}
return true;
}
export type Node<Markdown = string> =
| TextNode<Markdown>
| FileNode
| LinkNode
| GroupNode;
export function isNode(x: unknown): x is Node {
if (!isGenericNode(x)) {
return false;
}
return isTextNode(x) || isFileNode(x) || isLinkNode(x) || isGroupNode(x);
}
export type NodeSide = "top" | "right" | "bottom" | "left";
export function isNodeSide(x: unknown): x is NodeSide {
switch (x) {
case "top":
case "right":
case "bottom":
case "left":
return true;
default:
return false;
}
}
export type EdgeEnd = "none" | "arrow";
export function isEdgeEnd(x: unknown): x is EdgeEnd {
return x === "none" || x === "arrow";
}
export interface Edge {
/**
* A unique ID for the edge.
* NOTE: Length range and available characters are not defined in the spec.
*/
id: string;
/**
* The node `id` where the connection starts.
* NOTE: Pointing non-existent `id` is UB.
*/
fromNode: string;
/**
* The side where this edge starts.
* NOTE: UB when the field is empty.
*/
fromSide?: NodeSide;
/**
* The shape of the endpoint at the edge start.
* NOTE: UB when the field is empty.
*/
fromEnd?: EdgeEnd;
/**
* The node `id` where the connection ends.
* NOTE: Pointing non-existent `id` is UB.
*/
toNode: string;
/**
* The side where this edge ends.
* NOTE: UB when the field is empty.
*/
toSide?: NodeSide;
/**
* The shape of the endpoint at the edge end.
* NOTE: UB when the field is empty.
*/
toEnd?: EdgeEnd;
/**
* The color of the line.
*/
color?: CanvasColor;
/**
* A text label for the edge.
* NOTE: Length range and available characters are not defined in the spec.
*/
label?: string;
}
export function isEdge(x: unknown): x is Edge {
if (typeof x !== "object" || !x) {
return false;
}
if (!("id" in x) || typeof x.id !== "string") {
return false;
}
if (!("fromNode" in x) || typeof x.fromNode !== "string") {
return false;
}
if ("fromSide" in x && !isNodeSide(x.fromSide)) {
return false;
}
if ("fromEnd" in x && !isEdgeEnd(x.fromEnd)) {
return false;
}
if (!("toNode" in x) || typeof x.toNode !== "string") {
return false;
}
if ("toSide" in x && !isNodeSide(x.toSide)) {
return false;
}
if ("toEnd" in x && !isEdgeEnd(x.toEnd)) {
return false;
}
if ("color" in x && !isCanvasColor(x.color)) {
return false;
}
if ("label" in x && typeof x.label !== "string") {
return false;
}
return true;
}
export interface JSONCanvas<Markdown = string> {
nodes?: Node<Markdown>[];
edges?: Edge[];
}
export function isJSONCanvas(x: unknown): x is JSONCanvas {
if (typeof x !== "object" || !x) {
return false;
}
if ("nodes" in x && !(Array.isArray(x.nodes) && x.nodes.every(isNode))) {
return false;
}
if ("edges" in x && !(Array.isArray(x.edges) && x.edges.every(isEdge))) {
return false;
}
return true;
}