@agentshouse/mdmodel 0.1.0-ccf1367190878 → 0.1.0-cd5ace6e0b27a
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/API.md +232 -182
- package/README.md +21 -22
- package/dist/definition.d.ts +18 -1
- package/dist/definition.d.ts.map +1 -1
- package/dist/definition.js +176 -48
- package/dist/divide.d.ts.map +1 -1
- package/dist/divide.js +32 -21
- package/dist/document.d.ts +10 -6
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +424 -207
- package/dist/event.d.ts +78 -0
- package/dist/event.d.ts.map +1 -0
- package/dist/event.js +201 -0
- package/dist/facts.d.ts +15 -0
- package/dist/facts.d.ts.map +1 -0
- package/dist/facts.js +41 -0
- package/dist/form.d.ts +4 -0
- package/dist/form.d.ts.map +1 -0
- package/dist/form.js +139 -0
- package/dist/index.d.ts +25 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -8
- package/dist/kinds.d.ts +18 -0
- package/dist/kinds.d.ts.map +1 -0
- package/dist/kinds.js +150 -0
- package/dist/markdown.d.ts +51 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/markdown.js +271 -0
- package/dist/paths.d.ts +18 -2
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +69 -15
- package/dist/pattern.d.ts +10 -0
- package/dist/pattern.d.ts.map +1 -0
- package/dist/pattern.js +356 -0
- package/dist/project.d.ts +1 -0
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +44 -26
- package/dist/query/language.d.ts +254 -0
- package/dist/query/language.d.ts.map +1 -0
- package/dist/query/language.js +620 -0
- package/dist/query/match.d.ts +14 -0
- package/dist/query/match.d.ts.map +1 -0
- package/dist/query/match.js +150 -0
- package/dist/query/rows.d.ts +32 -0
- package/dist/query/rows.d.ts.map +1 -0
- package/dist/query/rows.js +161 -0
- package/dist/query/sql.d.ts +39 -0
- package/dist/query/sql.d.ts.map +1 -0
- package/dist/query/sql.js +285 -0
- package/dist/references.d.ts.map +1 -1
- package/dist/references.js +6 -1
- package/dist/reserved.d.ts +6 -0
- package/dist/reserved.d.ts.map +1 -0
- package/dist/reserved.js +21 -0
- package/dist/schema.d.ts +1 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +55 -4
- package/dist/types.d.ts +46 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +31 -4
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +387 -129
- package/dist/yaml.d.ts.map +1 -1
- package/dist/yaml.js +40 -14
- package/fixtures/README.md +34 -7
- package/fixtures/cases/conditional-section/context.json +1 -0
- package/fixtures/cases/conditional-section/expected.json +6 -0
- package/fixtures/cases/conditional-section/library/questions/hosting.md +9 -0
- package/fixtures/cases/conditional-section/library/questions/payments.md +12 -0
- package/fixtures/cases/conditional-section/schema.yaml +14 -0
- package/fixtures/cases/cross-room-link/context.json +1 -0
- package/fixtures/cases/cross-room-link/expected.json +1 -0
- package/fixtures/cases/cross-room-link/library/clients/acme.md +6 -0
- package/fixtures/cases/cross-room-link/schema.yaml +3 -0
- package/fixtures/cases/cross-room-reference/context.json +1 -0
- package/fixtures/cases/cross-room-reference/expected.json +1 -0
- package/fixtures/cases/cross-room-reference/library/clients/acme.md +9 -0
- package/fixtures/cases/cross-room-reference/schema.yaml +9 -0
- package/fixtures/cases/equal-labels/context.json +1 -0
- package/fixtures/cases/equal-labels/expected.json +5 -0
- package/fixtures/cases/equal-labels/schema.yaml +4 -0
- package/fixtures/cases/labels-in-the-rooms-language/context.json +1 -0
- package/fixtures/cases/labels-in-the-rooms-language/expected.json +1 -0
- package/fixtures/cases/labels-in-the-rooms-language/library/deals/atlas.md +6 -0
- package/fixtures/cases/labels-in-the-rooms-language/schema.yaml +9 -0
- package/fixtures/cases/lazy-list-item/context.json +1 -0
- package/fixtures/cases/lazy-list-item/expected.json +3 -0
- package/fixtures/cases/lazy-list-item/library/item.md +8 -0
- package/fixtures/cases/lazy-list-item/schema.yaml +3 -0
- package/fixtures/cases/link-into-a-reserved-name/context.json +1 -0
- package/fixtures/cases/link-into-a-reserved-name/expected.json +5 -0
- package/fixtures/cases/link-into-a-reserved-name/library/notes/a.md +6 -0
- package/fixtures/cases/link-into-a-reserved-name/schema.yaml +3 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/context.json +1 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/expected.json +5 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/library/notes/b.md +5 -0
- package/fixtures/cases/long-value-past-six-levels/context.json +1 -0
- package/fixtures/cases/long-value-past-six-levels/expected.json +5 -0
- package/fixtures/cases/long-value-past-six-levels/schema.yaml +15 -0
- package/fixtures/cases/member-on-an-untyped-document/context.json +1 -0
- package/fixtures/cases/member-on-an-untyped-document/expected.json +6 -0
- package/fixtures/cases/member-on-an-untyped-document/library/note.md +4 -0
- package/fixtures/cases/member-on-another-type/context.json +1 -0
- package/fixtures/cases/member-on-another-type/expected.json +6 -0
- package/fixtures/cases/member-on-another-type/library/note.md +4 -0
- package/fixtures/cases/member-on-another-type/schema.yaml +7 -0
- package/fixtures/cases/name-not-a-string/context.json +1 -0
- package/fixtures/cases/name-not-a-string/expected.json +5 -0
- package/fixtures/cases/name-not-a-string/schema.yaml +5 -0
- package/fixtures/cases/one-key-two-labels/context.json +1 -0
- package/fixtures/cases/one-key-two-labels/expected.json +5 -0
- package/fixtures/cases/one-key-two-labels/schema.yaml +5 -0
- package/fixtures/cases/one-key-two-types/context.json +1 -0
- package/fixtures/cases/one-key-two-types/expected.json +5 -0
- package/fixtures/cases/one-key-two-types/schema.yaml +4 -0
- package/fixtures/cases/reference-into-a-reserved-name/context.json +1 -0
- package/fixtures/cases/reference-into-a-reserved-name/expected.json +6 -0
- package/fixtures/cases/reference-into-a-reserved-name/library/notes/a.md +4 -0
- package/fixtures/cases/reference-into-a-reserved-name/library/settings/brief.md +1 -0
- package/fixtures/cases/reference-into-a-reserved-name/schema.yaml +3 -0
- package/fixtures/cases/room-md-declares-a-type/context.json +1 -0
- package/fixtures/cases/room-md-declares-a-type/expected.json +6 -0
- package/fixtures/cases/room-md-declares-a-type/library/ROOM.md +5 -0
- package/fixtures/cases/thematic-break-in-a-list-section/context.json +1 -0
- package/fixtures/cases/thematic-break-in-a-list-section/expected.json +6 -0
- package/fixtures/cases/thematic-break-in-a-list-section/library/item.md +7 -0
- package/fixtures/cases/thematic-break-in-a-list-section/schema.yaml +3 -0
- package/fixtures/cases/typed-path-names-another-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-another-type/expected.json +6 -0
- package/fixtures/cases/typed-path-names-another-type/library/settings/prefs/one.md +3 -0
- package/fixtures/cases/typed-path-names-another-type/schema.yaml +6 -0
- package/fixtures/cases/typed-path-names-its-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-its-type/expected.json +3 -0
- package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/deeper/two.md +1 -0
- package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/one.md +4 -0
- package/fixtures/cases/typed-path-names-its-type/schema.yaml +6 -0
- package/fixtures/cases/typed-path-names-no-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-no-type/expected.json +6 -0
- package/fixtures/cases/typed-path-names-no-type/library/settings/prefs/one.md +1 -0
- package/fixtures/cases/typed-path-names-no-type/schema.yaml +6 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/expected.json +6 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/library/rooms/team/tasks/plan.md +4 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/expected.json +6 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/library/rooms/team/tasks/plan.md +6 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/expected.json +3 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/plan.md +1 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/tasks/plan.md +5 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/expected.json +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/library/rooms/team/tasks/plan.md +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/schema.yaml +5 -0
- package/fixtures/division.json +73 -28
- package/fixtures/preamble.json +24 -3
- package/fixtures/projection.json +105 -2
- package/fixtures/queries.json +964 -0
- package/package.json +16 -2
- package/dist/blocks.d.ts +0 -11
- package/dist/blocks.d.ts.map +0 -1
- package/dist/blocks.js +0 -76
- package/dist/condition.d.ts +0 -149
- package/dist/condition.d.ts.map +0 -1
- package/dist/condition.js +0 -500
- package/dist/links.d.ts +0 -27
- package/dist/links.d.ts.map +0 -1
- package/dist/links.js +0 -774
- package/dist/profile.d.ts +0 -6
- package/dist/profile.d.ts.map +0 -1
- package/dist/profile.js +0 -5
- package/dist/sections.d.ts +0 -11
- package/dist/sections.d.ts.map +0 -1
- package/dist/sections.js +0 -57
package/dist/kinds.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { format } from '@cfworker/json-schema';
|
|
2
|
+
import { normalize } from './paths.js';
|
|
3
|
+
const DATE_TIME = /^(\d{4})-(\d{2})-(\d{2})[Tt\s](\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?(?:[Zz]|([+-])(\d{2})(?::?(\d{2}))?)$/u;
|
|
4
|
+
const BEFORE_YEAR_ZERO = 62200000000000000000n;
|
|
5
|
+
const INSTANT_DIGITS = 21;
|
|
6
|
+
export function kindOf(facts) {
|
|
7
|
+
if (facts.reference !== undefined)
|
|
8
|
+
return 'reference';
|
|
9
|
+
if (facts.values !== undefined)
|
|
10
|
+
return 'enum';
|
|
11
|
+
if (facts.type === 'number')
|
|
12
|
+
return 'number';
|
|
13
|
+
if (facts.type === 'integer')
|
|
14
|
+
return 'integer';
|
|
15
|
+
if (facts.type === 'boolean')
|
|
16
|
+
return 'boolean';
|
|
17
|
+
if (facts.type !== 'string')
|
|
18
|
+
return null;
|
|
19
|
+
if (facts.format === 'date')
|
|
20
|
+
return 'date';
|
|
21
|
+
if (facts.format === 'date-time')
|
|
22
|
+
return 'date-time';
|
|
23
|
+
if (facts.format === 'time')
|
|
24
|
+
return 'time';
|
|
25
|
+
return 'string';
|
|
26
|
+
}
|
|
27
|
+
export function fitsKind(kind, facts, value) {
|
|
28
|
+
if (kind === 'enum')
|
|
29
|
+
return (facts.values ?? []).includes(value);
|
|
30
|
+
if (kind === 'number')
|
|
31
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
32
|
+
if (kind === 'integer')
|
|
33
|
+
return Number.isSafeInteger(value);
|
|
34
|
+
if (kind === 'boolean')
|
|
35
|
+
return typeof value === 'boolean';
|
|
36
|
+
if (typeof value !== 'string')
|
|
37
|
+
return false;
|
|
38
|
+
if (kind === 'reference')
|
|
39
|
+
return value !== '' && normalize(value) === value;
|
|
40
|
+
if (kind === 'date')
|
|
41
|
+
return dated(value);
|
|
42
|
+
if (kind === 'date-time')
|
|
43
|
+
return instant(value) !== undefined;
|
|
44
|
+
if (kind === 'time')
|
|
45
|
+
return format.time(value);
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
export function kindWords(facts) {
|
|
49
|
+
const kind = facts === undefined ? null : kindOf(facts);
|
|
50
|
+
if (kind === 'enum')
|
|
51
|
+
return (facts.values ?? []).map((value) => typeof value === 'string' ? value : JSON.stringify(value));
|
|
52
|
+
if (kind === 'number')
|
|
53
|
+
return ['a number'];
|
|
54
|
+
if (kind === 'integer')
|
|
55
|
+
return ['an integer'];
|
|
56
|
+
if (kind === 'boolean')
|
|
57
|
+
return ['true', 'false'];
|
|
58
|
+
if (kind === 'date')
|
|
59
|
+
return ['a date as YYYY-MM-DD'];
|
|
60
|
+
if (kind === 'date-time')
|
|
61
|
+
return ['a date-time with its offset, such as 2026-10-10T09:30:00Z'];
|
|
62
|
+
if (kind === 'time')
|
|
63
|
+
return ['a time as HH:MM:SS'];
|
|
64
|
+
if (kind === 'reference')
|
|
65
|
+
return [`the Room Path of a ${facts.reference.join(' or ')} document, such as notes/a.md`];
|
|
66
|
+
return kind === 'string' ? ['a string'] : [];
|
|
67
|
+
}
|
|
68
|
+
export function dated(value) {
|
|
69
|
+
return typeof value === 'string' && format.date(value);
|
|
70
|
+
}
|
|
71
|
+
export function instant(value) {
|
|
72
|
+
if (typeof value !== 'string' || !format['date-time'](value))
|
|
73
|
+
return undefined;
|
|
74
|
+
const parts = DATE_TIME.exec(value);
|
|
75
|
+
const [year, month, day, hour, minute, second, offsetHours, offsetMinutes] = [
|
|
76
|
+
1, 2, 3, 4, 5, 6, 9, 10,
|
|
77
|
+
].map((at) => Number(parts[at] ?? 0));
|
|
78
|
+
const at = new Date(0);
|
|
79
|
+
at.setUTCFullYear(year, month - 1, day);
|
|
80
|
+
at.setUTCHours(hour, minute, second);
|
|
81
|
+
const offset = (parts[8] === '-' ? -1 : 1) * (offsetHours * 60 + offsetMinutes);
|
|
82
|
+
const fraction = (parts[7] ?? '').padEnd(9, '0').slice(0, 9);
|
|
83
|
+
return (BigInt(at.getTime()) - BigInt(offset) * 60000n) * 1000000n + BigInt(fraction);
|
|
84
|
+
}
|
|
85
|
+
export function instantKey(value) {
|
|
86
|
+
const at = instant(value);
|
|
87
|
+
return at === undefined ? null : (at + BEFORE_YEAR_ZERO).toString().padStart(INSTANT_DIGITS, '0');
|
|
88
|
+
}
|
|
89
|
+
function rank(value) {
|
|
90
|
+
if (typeof value === 'boolean' || typeof value === 'number')
|
|
91
|
+
return 1;
|
|
92
|
+
if (typeof value === 'string')
|
|
93
|
+
return 2;
|
|
94
|
+
return Array.isArray(value) ? 3 : 4;
|
|
95
|
+
}
|
|
96
|
+
function unit(code) {
|
|
97
|
+
if (code >= 0xd800 && code < 0xe000)
|
|
98
|
+
return code + 0x2000;
|
|
99
|
+
return code >= 0xe000 ? code - 0x800 : code;
|
|
100
|
+
}
|
|
101
|
+
export function byCodePoint(left, right) {
|
|
102
|
+
for (let at = 0; at < Math.min(left.length, right.length); at += 1) {
|
|
103
|
+
const ours = left.charCodeAt(at);
|
|
104
|
+
const theirs = right.charCodeAt(at);
|
|
105
|
+
if (ours !== theirs)
|
|
106
|
+
return unit(ours) - unit(theirs);
|
|
107
|
+
}
|
|
108
|
+
return left.length - right.length;
|
|
109
|
+
}
|
|
110
|
+
function ascending(left, right, ordering) {
|
|
111
|
+
if (left == null && right == null)
|
|
112
|
+
return 0;
|
|
113
|
+
if (left == null)
|
|
114
|
+
return ordering.nulls === 'first' ? -1 : 1;
|
|
115
|
+
if (right == null)
|
|
116
|
+
return ordering.nulls === 'first' ? 1 : -1;
|
|
117
|
+
const ranked = rank(left) - rank(right);
|
|
118
|
+
if (ranked !== 0)
|
|
119
|
+
return ranked;
|
|
120
|
+
if (typeof left === 'string' && typeof right === 'string') {
|
|
121
|
+
return ordering.stringSort === 'locale'
|
|
122
|
+
? left.localeCompare(right, ordering.locale, ordering.localeOptions)
|
|
123
|
+
: byCodePoint(left, right);
|
|
124
|
+
}
|
|
125
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
126
|
+
for (let at = 0; at < Math.min(left.length, right.length); at += 1) {
|
|
127
|
+
const result = ascending(left[at], right[at], ordering);
|
|
128
|
+
if (result !== 0)
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
return left.length - right.length;
|
|
132
|
+
}
|
|
133
|
+
if (typeof left === 'object')
|
|
134
|
+
return 0;
|
|
135
|
+
const held = left;
|
|
136
|
+
const other = right;
|
|
137
|
+
if (held < other)
|
|
138
|
+
return -1;
|
|
139
|
+
if (held > other)
|
|
140
|
+
return 1;
|
|
141
|
+
return 0;
|
|
142
|
+
}
|
|
143
|
+
export function compared(left, right, ordering) {
|
|
144
|
+
if (ordering.direction === 'asc')
|
|
145
|
+
return ascending(left, right, ordering);
|
|
146
|
+
return ascending(right, left, {
|
|
147
|
+
...ordering,
|
|
148
|
+
nulls: ordering.nulls === 'first' ? 'last' : 'first',
|
|
149
|
+
});
|
|
150
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Extent } from './types.js';
|
|
2
|
+
export type HeadingLine = {
|
|
3
|
+
readonly level: number;
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly start: number;
|
|
6
|
+
readonly textEnd: number;
|
|
7
|
+
readonly next: number;
|
|
8
|
+
};
|
|
9
|
+
export type HrefOccurrence = {
|
|
10
|
+
readonly href: string;
|
|
11
|
+
readonly start: number;
|
|
12
|
+
readonly end: number;
|
|
13
|
+
readonly bracketed: boolean;
|
|
14
|
+
};
|
|
15
|
+
export type LinkUse = {
|
|
16
|
+
readonly start: number;
|
|
17
|
+
readonly end: number;
|
|
18
|
+
readonly labelStart: number;
|
|
19
|
+
readonly labelEnd: number;
|
|
20
|
+
readonly destination: HrefOccurrence;
|
|
21
|
+
};
|
|
22
|
+
export type DefinitionBlock = {
|
|
23
|
+
readonly start: number;
|
|
24
|
+
readonly end: number;
|
|
25
|
+
readonly destination: HrefOccurrence;
|
|
26
|
+
};
|
|
27
|
+
export type Block = Extent & {
|
|
28
|
+
readonly items: readonly Extent[] | null;
|
|
29
|
+
};
|
|
30
|
+
export type Reading = {
|
|
31
|
+
readonly headings: readonly HeadingLine[];
|
|
32
|
+
readonly blocks: readonly Block[];
|
|
33
|
+
readonly uses: readonly LinkUse[];
|
|
34
|
+
readonly definitions: readonly DefinitionBlock[];
|
|
35
|
+
};
|
|
36
|
+
export declare function read(text: string, at?: number): Reading;
|
|
37
|
+
export declare function destinations(reading: Reading): readonly HrefOccurrence[];
|
|
38
|
+
export declare function hrefOccurrences(body: string): readonly HrefOccurrence[];
|
|
39
|
+
export declare function headingLines(content: string, from: number, to: number): HeadingLine[];
|
|
40
|
+
export type SectionSpan = {
|
|
41
|
+
readonly name: string;
|
|
42
|
+
readonly headingStart: number;
|
|
43
|
+
readonly bodyStart: number;
|
|
44
|
+
readonly bodyEnd: number;
|
|
45
|
+
};
|
|
46
|
+
export declare function sectionSpans(reading: Reading, end: number): SectionSpan[];
|
|
47
|
+
export declare function sectionsRecord(spans: readonly SectionSpan[], content: string): Record<string, string>;
|
|
48
|
+
export declare function headingsIn(reading: Reading, from: number, to: number): readonly HeadingLine[];
|
|
49
|
+
export declare function listItems(reading: Reading, from: number, to: number): Extent[] | null;
|
|
50
|
+
export declare function blockRanges(reading: Reading, from: number, to: number): Extent[];
|
|
51
|
+
//# sourceMappingURL=markdown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../src/markdown.ts"],"names":[],"mappings":"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"}
|
package/dist/markdown.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { fromMarkdown, } from 'mdast-util-from-markdown';
|
|
2
|
+
import { gfmFootnoteFromMarkdown } from 'mdast-util-gfm-footnote';
|
|
3
|
+
import { gfmStrikethroughFromMarkdown } from 'mdast-util-gfm-strikethrough';
|
|
4
|
+
import { gfmTableFromMarkdown } from 'mdast-util-gfm-table';
|
|
5
|
+
import { gfmTaskListItemFromMarkdown } from 'mdast-util-gfm-task-list-item';
|
|
6
|
+
import { gfmFootnote } from 'micromark-extension-gfm-footnote';
|
|
7
|
+
import { gfmStrikethrough } from 'micromark-extension-gfm-strikethrough';
|
|
8
|
+
import { gfmTable } from 'micromark-extension-gfm-table';
|
|
9
|
+
import { gfmTaskListItem } from 'micromark-extension-gfm-task-list-item';
|
|
10
|
+
const BYTE_ORDER_MARK = 0xfeff;
|
|
11
|
+
const LINKED = new Set(['link', 'image', 'linkReference', 'imageReference']);
|
|
12
|
+
const REFERENCED = new Set(['linkReference', 'imageReference']);
|
|
13
|
+
const SYNTAX = [gfmFootnote(), gfmStrikethrough(), gfmTable(), gfmTaskListItem()];
|
|
14
|
+
function startOf(node) {
|
|
15
|
+
return node.position.start.offset;
|
|
16
|
+
}
|
|
17
|
+
function endOf(node) {
|
|
18
|
+
return node.position.end.offset;
|
|
19
|
+
}
|
|
20
|
+
function marked(marks, node) {
|
|
21
|
+
let mark = marks.get(node);
|
|
22
|
+
if (mark === undefined) {
|
|
23
|
+
mark = {};
|
|
24
|
+
marks.set(node, mark);
|
|
25
|
+
}
|
|
26
|
+
return mark;
|
|
27
|
+
}
|
|
28
|
+
function marking(text, shift, marks, labelled) {
|
|
29
|
+
const at = (point) => point.offset + shift;
|
|
30
|
+
return {
|
|
31
|
+
afterExit(token) {
|
|
32
|
+
const top = this.stack[this.stack.length - 1];
|
|
33
|
+
const start = at(token.start);
|
|
34
|
+
const end = at(token.end);
|
|
35
|
+
switch (token.type) {
|
|
36
|
+
case 'labelText': {
|
|
37
|
+
const node = this.stack[this.stack.length - 2];
|
|
38
|
+
marked(marks, node).label = { start, end };
|
|
39
|
+
labelled.push(node);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
case 'resourceDestinationLiteral':
|
|
43
|
+
case 'definitionDestinationLiteral':
|
|
44
|
+
marked(marks, top).destination = {
|
|
45
|
+
href: text.slice(start + 1, end - 1),
|
|
46
|
+
start,
|
|
47
|
+
end,
|
|
48
|
+
bracketed: true,
|
|
49
|
+
};
|
|
50
|
+
return;
|
|
51
|
+
case 'resourceDestinationRaw':
|
|
52
|
+
case 'definitionDestinationRaw':
|
|
53
|
+
marked(marks, top).destination = {
|
|
54
|
+
href: text.slice(start, end),
|
|
55
|
+
start,
|
|
56
|
+
end,
|
|
57
|
+
bracketed: false,
|
|
58
|
+
};
|
|
59
|
+
return;
|
|
60
|
+
case 'resource':
|
|
61
|
+
marked(marks, top).resourceEnd = end;
|
|
62
|
+
return;
|
|
63
|
+
case 'atxHeadingText':
|
|
64
|
+
case 'setextHeadingText':
|
|
65
|
+
marked(marks, top).text = { start, end };
|
|
66
|
+
return;
|
|
67
|
+
case 'listItemPrefix':
|
|
68
|
+
marked(marks, top).content = end;
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function lineStart(text, index) {
|
|
75
|
+
let at = index;
|
|
76
|
+
while (at > 0 && text[at - 1] !== '\n' && text[at - 1] !== '\r')
|
|
77
|
+
at -= 1;
|
|
78
|
+
return at;
|
|
79
|
+
}
|
|
80
|
+
function lineEnd(text, index) {
|
|
81
|
+
let at = index;
|
|
82
|
+
while (at < text.length && text[at] !== '\n' && text[at] !== '\r')
|
|
83
|
+
at += 1;
|
|
84
|
+
if (at === text.length)
|
|
85
|
+
return { end: at, next: at };
|
|
86
|
+
return { end: at, next: text[at] === '\r' && text[at + 1] === '\n' ? at + 2 : at + 1 };
|
|
87
|
+
}
|
|
88
|
+
function trimmed(text, start, end) {
|
|
89
|
+
let from = start;
|
|
90
|
+
let to = end;
|
|
91
|
+
while (from < to && /\s/.test(text[from]))
|
|
92
|
+
from += 1;
|
|
93
|
+
while (to > from && /\s/.test(text[to - 1]))
|
|
94
|
+
to -= 1;
|
|
95
|
+
return { start: from, end: to };
|
|
96
|
+
}
|
|
97
|
+
function shifted(value, by) {
|
|
98
|
+
return { ...value, start: value.start + by, end: value.end + by };
|
|
99
|
+
}
|
|
100
|
+
function walked(root, visit) {
|
|
101
|
+
const pending = [root];
|
|
102
|
+
while (pending.length > 0) {
|
|
103
|
+
const node = pending.pop();
|
|
104
|
+
visit(node);
|
|
105
|
+
const children = node.children ?? [];
|
|
106
|
+
for (let index = children.length - 1; index >= 0; index -= 1)
|
|
107
|
+
pending.push(children[index]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export function read(text, at = 0) {
|
|
111
|
+
const marks = new WeakMap();
|
|
112
|
+
const labelled = [];
|
|
113
|
+
const shift = text.charCodeAt(0) === BYTE_ORDER_MARK ? 1 : 0;
|
|
114
|
+
const root = fromMarkdown(text, {
|
|
115
|
+
extensions: SYNTAX,
|
|
116
|
+
mdastExtensions: [
|
|
117
|
+
gfmFootnoteFromMarkdown(),
|
|
118
|
+
gfmStrikethroughFromMarkdown(),
|
|
119
|
+
gfmTableFromMarkdown(),
|
|
120
|
+
gfmTaskListItemFromMarkdown(),
|
|
121
|
+
marking(text, shift, marks, labelled),
|
|
122
|
+
],
|
|
123
|
+
});
|
|
124
|
+
const local = (offset) => offset + shift;
|
|
125
|
+
const headings = [];
|
|
126
|
+
const blocks = [];
|
|
127
|
+
for (const node of root.children ?? []) {
|
|
128
|
+
let start = local(startOf(node));
|
|
129
|
+
while (start > 0 && (text[start - 1] === ' ' || text[start - 1] === '\t'))
|
|
130
|
+
start -= 1;
|
|
131
|
+
const end = local(endOf(node));
|
|
132
|
+
if (node.type === 'heading') {
|
|
133
|
+
const name = marks.get(node)?.text;
|
|
134
|
+
const line = lineEnd(text, end);
|
|
135
|
+
headings.push({
|
|
136
|
+
level: node.depth,
|
|
137
|
+
name: name === undefined ? '' : text.slice(name.start, name.end),
|
|
138
|
+
start: at + start,
|
|
139
|
+
textEnd: at + line.end,
|
|
140
|
+
next: at + line.next,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
blocks.push({
|
|
144
|
+
start: at + start,
|
|
145
|
+
end: at + end,
|
|
146
|
+
items: node.type === 'list'
|
|
147
|
+
? (node.children ?? []).map((item) => shifted(trimmed(text, marks.get(item)?.content ?? local(startOf(item)), local(endOf(item))), at))
|
|
148
|
+
: null,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
const destinations = new Map();
|
|
152
|
+
const definitions = [];
|
|
153
|
+
walked(root, (node) => {
|
|
154
|
+
const mark = marks.get(node);
|
|
155
|
+
if (node.type !== 'definition' || mark?.destination === undefined)
|
|
156
|
+
return;
|
|
157
|
+
const destination = shifted(mark.destination, at);
|
|
158
|
+
if (!destinations.has(node.identifier))
|
|
159
|
+
destinations.set(node.identifier, destination);
|
|
160
|
+
definitions.push({
|
|
161
|
+
start: at + lineStart(text, local(startOf(node))),
|
|
162
|
+
end: at + lineEnd(text, local(endOf(node))).next,
|
|
163
|
+
destination,
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
const uses = [];
|
|
167
|
+
for (const node of labelled) {
|
|
168
|
+
if (!LINKED.has(node.type))
|
|
169
|
+
continue;
|
|
170
|
+
const mark = marks.get(node);
|
|
171
|
+
const destination = REFERENCED.has(node.type)
|
|
172
|
+
? destinations.get(node.identifier)
|
|
173
|
+
: mark.destination !== undefined
|
|
174
|
+
? shifted(mark.destination, at)
|
|
175
|
+
: mark.resourceEnd === undefined
|
|
176
|
+
? undefined
|
|
177
|
+
: {
|
|
178
|
+
href: '',
|
|
179
|
+
start: at + mark.resourceEnd - 1,
|
|
180
|
+
end: at + mark.resourceEnd - 1,
|
|
181
|
+
bracketed: false,
|
|
182
|
+
};
|
|
183
|
+
if (destination === undefined)
|
|
184
|
+
continue;
|
|
185
|
+
uses.push({
|
|
186
|
+
start: at + local(startOf(node)),
|
|
187
|
+
end: at + local(endOf(node)),
|
|
188
|
+
labelStart: at + mark.label.start,
|
|
189
|
+
labelEnd: at + mark.label.end,
|
|
190
|
+
destination,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
uses.sort((left, right) => left.start - right.start);
|
|
194
|
+
return { headings, blocks, uses, definitions };
|
|
195
|
+
}
|
|
196
|
+
export function destinations(reading) {
|
|
197
|
+
const byStart = new Map();
|
|
198
|
+
for (const use of reading.uses)
|
|
199
|
+
byStart.set(use.destination.start, use.destination);
|
|
200
|
+
return [...byStart.values()].sort((left, right) => left.start - right.start);
|
|
201
|
+
}
|
|
202
|
+
export function hrefOccurrences(body) {
|
|
203
|
+
return destinations(read(body));
|
|
204
|
+
}
|
|
205
|
+
export function headingLines(content, from, to) {
|
|
206
|
+
return [...read(content.slice(from, to), from).headings];
|
|
207
|
+
}
|
|
208
|
+
export function sectionSpans(reading, end) {
|
|
209
|
+
const found = [];
|
|
210
|
+
for (const heading of reading.headings) {
|
|
211
|
+
if (heading.level !== 2)
|
|
212
|
+
continue;
|
|
213
|
+
if (found.length > 0)
|
|
214
|
+
found[found.length - 1] = { ...found[found.length - 1], bodyEnd: heading.start };
|
|
215
|
+
found.push({
|
|
216
|
+
name: heading.name,
|
|
217
|
+
headingStart: heading.start,
|
|
218
|
+
bodyStart: heading.next,
|
|
219
|
+
bodyEnd: end,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
return found;
|
|
223
|
+
}
|
|
224
|
+
export function sectionsRecord(spans, content) {
|
|
225
|
+
const record = Object.create(null);
|
|
226
|
+
for (const span of spans) {
|
|
227
|
+
if (record[span.name] === undefined)
|
|
228
|
+
record[span.name] = content.slice(span.bodyStart, span.bodyEnd);
|
|
229
|
+
}
|
|
230
|
+
return record;
|
|
231
|
+
}
|
|
232
|
+
function firstPast(items, at, key) {
|
|
233
|
+
let low = 0;
|
|
234
|
+
let high = items.length;
|
|
235
|
+
while (low < high) {
|
|
236
|
+
const middle = (low + high) >>> 1;
|
|
237
|
+
if (key(items[middle]) <= at)
|
|
238
|
+
low = middle + 1;
|
|
239
|
+
else
|
|
240
|
+
high = middle;
|
|
241
|
+
}
|
|
242
|
+
return low;
|
|
243
|
+
}
|
|
244
|
+
function blocksIn(reading, from, to) {
|
|
245
|
+
const blocks = reading.blocks;
|
|
246
|
+
const first = firstPast(blocks, from, (block) => block.end);
|
|
247
|
+
let end = first;
|
|
248
|
+
while (end < blocks.length && blocks[end].start < to)
|
|
249
|
+
end += 1;
|
|
250
|
+
return blocks.slice(first, end);
|
|
251
|
+
}
|
|
252
|
+
export function headingsIn(reading, from, to) {
|
|
253
|
+
const start = (heading) => heading.start;
|
|
254
|
+
return reading.headings.slice(firstPast(reading.headings, from - 1, start), firstPast(reading.headings, to - 1, start));
|
|
255
|
+
}
|
|
256
|
+
export function listItems(reading, from, to) {
|
|
257
|
+
const items = [];
|
|
258
|
+
for (const block of blocksIn(reading, from, to)) {
|
|
259
|
+
if (block.items === null)
|
|
260
|
+
return null;
|
|
261
|
+
items.push(...block.items);
|
|
262
|
+
}
|
|
263
|
+
return items;
|
|
264
|
+
}
|
|
265
|
+
export function blockRanges(reading, from, to) {
|
|
266
|
+
const starts = blocksIn(reading, from, to).map((block) => block.start);
|
|
267
|
+
return starts.map((start, index) => ({
|
|
268
|
+
start: index === 0 ? from : start,
|
|
269
|
+
end: starts[index + 1] ?? to,
|
|
270
|
+
}));
|
|
271
|
+
}
|
package/dist/paths.d.ts
CHANGED
|
@@ -2,17 +2,33 @@ export declare function dirname(path: string): string;
|
|
|
2
2
|
export declare function join(base: string, relative: string): string;
|
|
3
3
|
export declare function normalize(path: string): string | null;
|
|
4
4
|
export declare function destinationPath(href: string): string | null;
|
|
5
|
-
export
|
|
5
|
+
export type RoomTarget = {
|
|
6
|
+
readonly handle: string;
|
|
7
|
+
readonly path: string;
|
|
8
|
+
};
|
|
9
|
+
type Classified = {
|
|
6
10
|
kind: 'skip';
|
|
7
11
|
} | {
|
|
8
12
|
kind: 'path';
|
|
9
13
|
path: string;
|
|
10
|
-
} | {
|
|
14
|
+
} | ({
|
|
15
|
+
kind: 'room';
|
|
16
|
+
} & RoomTarget) | {
|
|
11
17
|
kind: 'invalid';
|
|
12
18
|
};
|
|
19
|
+
export declare function classifyHref(from: string, href: string): Classified;
|
|
20
|
+
export declare function roomTarget(href: string): RoomTarget | null;
|
|
21
|
+
export declare function roomPath(href: string): string | null;
|
|
13
22
|
export declare function target(from: string, href: string): string | null;
|
|
23
|
+
export interface LinkedPath {
|
|
24
|
+
readonly handle: string | null;
|
|
25
|
+
readonly path: string;
|
|
26
|
+
readonly fragment: string | null;
|
|
27
|
+
}
|
|
28
|
+
export declare function linkedPath(from: string, href: string): LinkedPath | null;
|
|
14
29
|
export declare function retarget(href: string, to: string): string;
|
|
15
30
|
export declare function relative(from: string, target: string): string;
|
|
16
31
|
export declare const SHELL_ROOMS = "/rooms/";
|
|
17
32
|
export declare function shellTarget(href: string, handle: string): string | null;
|
|
33
|
+
export {};
|
|
18
34
|
//# sourceMappingURL=paths.d.ts.map
|
package/dist/paths.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAUA,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG5C;AAED,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAG3D;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAYrD;AA6DD,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAW3D;AAgBD,MAAM,MAAM,UAAU,GAAG;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAiB5E,KAAK,UAAU,GACX;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAChB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9B,CAAC;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,UAAU,CAAC,GAC/B;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,CAAC;AAgBxB,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,CAGnE;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAG1D;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAGpD;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAGhE;AAED,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CAClC;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAaxE;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,CAGzD;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAW7D;AAED,eAAO,MAAM,WAAW,YAAY,CAAC;AAErC,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAMvE"}
|
package/dist/paths.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { decodeNamedCharacterReference } from 'decode-named-character-reference';
|
|
2
2
|
import { decodeNumericCharacterReference } from 'micromark-util-decode-numeric-character-reference';
|
|
3
|
-
|
|
3
|
+
const BACKSLASH = 0x5c;
|
|
4
|
+
const PUNCTUATION = /^[!-/:-@[-`{-~]$/;
|
|
5
|
+
function escapes(text, index) {
|
|
6
|
+
return text.charCodeAt(index) === BACKSLASH && PUNCTUATION.test(text[index + 1] ?? '');
|
|
7
|
+
}
|
|
4
8
|
export function dirname(path) {
|
|
5
9
|
const index = path.lastIndexOf('/');
|
|
6
10
|
return index === -1 ? '' : path.slice(0, index);
|
|
@@ -43,25 +47,32 @@ function referenceAt(text, at) {
|
|
|
43
47
|
end,
|
|
44
48
|
};
|
|
45
49
|
}
|
|
46
|
-
function
|
|
47
|
-
let
|
|
48
|
-
let i =
|
|
50
|
+
function spelled(written, from, stops) {
|
|
51
|
+
let text = '';
|
|
52
|
+
let i = from;
|
|
49
53
|
while (i < written.length) {
|
|
50
54
|
const escaped = escapes(written, i);
|
|
51
55
|
const reference = escaped || written[i] !== '&' ? null : referenceAt(written, i);
|
|
52
|
-
const
|
|
53
|
-
if (
|
|
54
|
-
return {
|
|
55
|
-
|
|
56
|
+
const one = escaped ? written[i + 1] : (reference?.text ?? written[i]);
|
|
57
|
+
if (stops && (one.startsWith('?') || one.startsWith('#')))
|
|
58
|
+
return { text, end: i };
|
|
59
|
+
text += one;
|
|
56
60
|
i = escaped ? i + 2 : (reference?.end ?? i + 1);
|
|
57
61
|
}
|
|
58
|
-
return {
|
|
62
|
+
return { text, end: written.length };
|
|
63
|
+
}
|
|
64
|
+
function unescaped(written) {
|
|
65
|
+
const { text, end } = spelled(written, 0, true);
|
|
66
|
+
return { base: text, suffix: end };
|
|
59
67
|
}
|
|
60
68
|
function located(href) {
|
|
61
69
|
const written = href.trim();
|
|
62
70
|
const { base, suffix } = unescaped(written);
|
|
63
71
|
const url = base.replace(/[\t\n\r]/g, '').replace(/^[\u0000-\u0020]+/, '');
|
|
64
|
-
return {
|
|
72
|
+
return {
|
|
73
|
+
base: suffix === written.length ? url.replace(/(?<![\u0000-\u0020])[\u0000-\u0020]+$/, '') : url,
|
|
74
|
+
suffix,
|
|
75
|
+
};
|
|
65
76
|
}
|
|
66
77
|
const ENCODED = /(?:%[0-9a-fA-F]{2})+/g;
|
|
67
78
|
function decodedRun(run) {
|
|
@@ -104,24 +115,67 @@ function written(path) {
|
|
|
104
115
|
}
|
|
105
116
|
return text;
|
|
106
117
|
}
|
|
107
|
-
|
|
118
|
+
function roomAt(path) {
|
|
119
|
+
if (!path.startsWith(SHELL_ROOMS))
|
|
120
|
+
return null;
|
|
121
|
+
const rest = path.slice(SHELL_ROOMS.length);
|
|
122
|
+
const slash = rest.indexOf('/');
|
|
123
|
+
const handle = slash === -1 ? rest : rest.slice(0, slash);
|
|
124
|
+
if (handle === '' || handle === '.' || handle === '..')
|
|
125
|
+
return null;
|
|
126
|
+
const within = normalize(slash === -1 ? '' : rest.slice(slash + 1));
|
|
127
|
+
return within === null ? null : { handle, path: within };
|
|
128
|
+
}
|
|
129
|
+
function roomTargetOf(path) {
|
|
130
|
+
const room = roomAt(path);
|
|
131
|
+
return room === null || room.path === '' ? null : room;
|
|
132
|
+
}
|
|
133
|
+
function resolvedOf(from, href) {
|
|
108
134
|
const base = located(href).base;
|
|
109
135
|
if (base === '')
|
|
110
136
|
return { kind: 'skip' };
|
|
111
137
|
if (/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(base))
|
|
112
138
|
return { kind: 'skip' };
|
|
113
139
|
const path = destinationPath(href);
|
|
114
|
-
if (path === null
|
|
140
|
+
if (path === null)
|
|
115
141
|
return { kind: 'invalid' };
|
|
142
|
+
if (path.startsWith('/')) {
|
|
143
|
+
const room = roomAt(path);
|
|
144
|
+
return room === null ? { kind: 'invalid' } : { kind: 'room', ...room };
|
|
145
|
+
}
|
|
116
146
|
const resolved = normalize(join(dirname(from), path));
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
147
|
+
return resolved === null ? { kind: 'invalid' } : { kind: 'path', path: resolved };
|
|
148
|
+
}
|
|
149
|
+
export function classifyHref(from, href) {
|
|
150
|
+
const resolved = resolvedOf(from, href);
|
|
151
|
+
return 'path' in resolved && resolved.path === '' ? { kind: 'invalid' } : resolved;
|
|
152
|
+
}
|
|
153
|
+
export function roomTarget(href) {
|
|
154
|
+
const path = destinationPath(href);
|
|
155
|
+
return path === null ? null : roomTargetOf(path);
|
|
156
|
+
}
|
|
157
|
+
export function roomPath(href) {
|
|
158
|
+
const room = roomTarget(href);
|
|
159
|
+
return room === null ? null : `${SHELL_ROOMS}${room.handle}/${room.path}`;
|
|
120
160
|
}
|
|
121
161
|
export function target(from, href) {
|
|
122
162
|
const resolved = classifyHref(from, href);
|
|
123
163
|
return resolved.kind === 'path' ? resolved.path : null;
|
|
124
164
|
}
|
|
165
|
+
export function linkedPath(from, href) {
|
|
166
|
+
const resolved = located(href).base === '' ? { kind: 'path', path: from } : resolvedOf(from, href);
|
|
167
|
+
if (!('path' in resolved))
|
|
168
|
+
return null;
|
|
169
|
+
const written = href.trim();
|
|
170
|
+
const suffix = spelled(written, unescaped(written).suffix, false).text;
|
|
171
|
+
const hash = suffix.indexOf('#');
|
|
172
|
+
const fragment = hash === -1 ? '' : suffix.slice(hash + 1);
|
|
173
|
+
return {
|
|
174
|
+
handle: resolved.kind === 'room' ? resolved.handle : null,
|
|
175
|
+
path: resolved.path,
|
|
176
|
+
fragment: fragment === '' ? null : (decodedRun(fragment) ?? fragment),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
125
179
|
export function retarget(href, to) {
|
|
126
180
|
const trimmed = href.trim();
|
|
127
181
|
return written(to) + trimmed.slice(unescaped(trimmed).suffix);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const PATTERN_STATES = 1000;
|
|
2
|
+
export declare class PatternTooCostly extends Error {
|
|
3
|
+
}
|
|
4
|
+
export type Pattern = {
|
|
5
|
+
readonly test: (text: string) => boolean;
|
|
6
|
+
};
|
|
7
|
+
export declare function linearPattern(source: string): Pattern | null;
|
|
8
|
+
export declare function withLinearRegExp<T>(run: () => T): T;
|
|
9
|
+
export declare function openPatternBudget(): () => void;
|
|
10
|
+
//# sourceMappingURL=pattern.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pattern.d.ts","sourceRoot":"","sources":["../src/pattern.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,cAAc,OAAQ,CAAC;AAMpC,qBAAa,gBAAiB,SAAQ,KAAK;CAAG;AAQ9C,MAAM,MAAM,OAAO,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAA;CAAE,CAAC;AAkUnE,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAM5D;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC,CAkBnD;AAED,wBAAgB,iBAAiB,IAAI,MAAM,IAAI,CAM9C"}
|