@kelpie/ui 0.16.0 → 0.18.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/dist/api/client.d.ts +1 -1
- package/dist/api/resources/ai.d.ts +21 -0
- package/dist/api/resources/ai.d.ts.map +1 -0
- package/dist/api/resources/ai.js +101 -0
- package/dist/api/resources/ai.js.map +1 -0
- package/dist/api/resources/conversions.js +1 -1
- package/dist/api/resources/conversions.js.map +1 -1
- package/dist/api/resources/forms.d.ts +8 -0
- package/dist/api/resources/forms.d.ts.map +1 -1
- package/dist/api/resources/forms.js +39 -0
- package/dist/api/resources/forms.js.map +1 -1
- package/dist/api/resources/personIntake.d.ts +12 -0
- package/dist/api/resources/personIntake.d.ts.map +1 -0
- package/dist/api/resources/personIntake.js +54 -0
- package/dist/api/resources/personIntake.js.map +1 -0
- package/dist/api/resources/session.d.ts.map +1 -1
- package/dist/api/resources/session.js +3 -1
- package/dist/api/resources/session.js.map +1 -1
- package/dist/api/resources/workspace.d.ts +2 -2
- package/dist/api/resources/workspace.js +2 -2
- package/dist/app/KelpieApp.js +2 -2
- package/dist/app/KelpieApp.js.map +1 -1
- package/dist/components/ActivitiesPanel.d.ts +7 -4
- package/dist/components/ActivitiesPanel.d.ts.map +1 -1
- package/dist/components/ActivitiesPanel.js +12 -2
- package/dist/components/ActivitiesPanel.js.map +1 -1
- package/dist/components/ActivityAction.d.ts +15 -0
- package/dist/components/ActivityAction.d.ts.map +1 -0
- package/dist/components/ActivityAction.js +20 -0
- package/dist/components/ActivityAction.js.map +1 -0
- package/dist/components/AgentTasks.d.ts +5 -0
- package/dist/components/AgentTasks.d.ts.map +1 -1
- package/dist/components/AgentTasks.js +88 -10
- package/dist/components/AgentTasks.js.map +1 -1
- package/dist/components/DecisionsPanel.d.ts.map +1 -1
- package/dist/components/DecisionsPanel.js +3 -1
- package/dist/components/DecisionsPanel.js.map +1 -1
- package/dist/components/LinkedText.d.ts +12 -0
- package/dist/components/LinkedText.d.ts.map +1 -0
- package/dist/components/LinkedText.js +25 -0
- package/dist/components/LinkedText.js.map +1 -0
- package/dist/components/MarkdownView.d.ts +3 -1
- package/dist/components/MarkdownView.d.ts.map +1 -1
- package/dist/components/MarkdownView.js +17 -3
- package/dist/components/MarkdownView.js.map +1 -1
- package/dist/components/NotesPanel.d.ts +2 -1
- package/dist/components/NotesPanel.d.ts.map +1 -1
- package/dist/components/NotesPanel.js +6 -7
- package/dist/components/NotesPanel.js.map +1 -1
- package/dist/components/PersonIntakeDialog.d.ts +19 -0
- package/dist/components/PersonIntakeDialog.d.ts.map +1 -0
- package/dist/components/PersonIntakeDialog.js +87 -0
- package/dist/components/PersonIntakeDialog.js.map +1 -0
- package/dist/components/PlanPanel.d.ts.map +1 -1
- package/dist/components/PlanPanel.js +3 -1
- package/dist/components/PlanPanel.js.map +1 -1
- package/dist/components/RecordLinkTextarea.d.ts +12 -0
- package/dist/components/RecordLinkTextarea.d.ts.map +1 -0
- package/dist/components/RecordLinkTextarea.js +142 -0
- package/dist/components/RecordLinkTextarea.js.map +1 -0
- package/dist/components/Shell.js +1 -1
- package/dist/components/Shell.js.map +1 -1
- package/dist/components/WorkspaceSwitcher.d.ts +2 -1
- package/dist/components/WorkspaceSwitcher.d.ts.map +1 -1
- package/dist/components/WorkspaceSwitcher.js +7 -2
- package/dist/components/WorkspaceSwitcher.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/fragmentTarget.d.ts +18 -0
- package/dist/lib/fragmentTarget.d.ts.map +1 -0
- package/dist/lib/fragmentTarget.js +73 -0
- package/dist/lib/fragmentTarget.js.map +1 -0
- package/dist/lib/recordLinks.d.ts +17 -0
- package/dist/lib/recordLinks.d.ts.map +1 -0
- package/dist/lib/recordLinks.js +48 -0
- package/dist/lib/recordLinks.js.map +1 -0
- package/dist/lib/recordReferences.d.ts +41 -0
- package/dist/lib/recordReferences.d.ts.map +1 -0
- package/dist/lib/recordReferences.js +92 -0
- package/dist/lib/recordReferences.js.map +1 -0
- package/dist/modules/PersonIntakeWizard.d.ts +3 -0
- package/dist/modules/PersonIntakeWizard.d.ts.map +1 -0
- package/dist/modules/PersonIntakeWizard.js +223 -0
- package/dist/modules/PersonIntakeWizard.js.map +1 -0
- package/dist/modules/ai.d.ts +14 -0
- package/dist/modules/ai.d.ts.map +1 -0
- package/dist/modules/ai.js +26 -0
- package/dist/modules/ai.js.map +1 -0
- package/dist/modules/aiRunner.d.ts +3 -0
- package/dist/modules/aiRunner.d.ts.map +1 -0
- package/dist/modules/aiRunner.js +68 -0
- package/dist/modules/aiRunner.js.map +1 -0
- package/dist/modules/personIntakeSelection.d.ts +7 -0
- package/dist/modules/personIntakeSelection.d.ts.map +1 -0
- package/dist/modules/personIntakeSelection.js +21 -0
- package/dist/modules/personIntakeSelection.js.map +1 -0
- package/dist/pages/CompanyDetail.d.ts.map +1 -1
- package/dist/pages/CompanyDetail.js +2 -1
- package/dist/pages/CompanyDetail.js.map +1 -1
- package/dist/pages/DealDetail.d.ts.map +1 -1
- package/dist/pages/DealDetail.js +2 -1
- package/dist/pages/DealDetail.js.map +1 -1
- package/dist/pages/EnquiryDetail.d.ts.map +1 -1
- package/dist/pages/EnquiryDetail.js +2 -1
- package/dist/pages/EnquiryDetail.js.map +1 -1
- package/dist/pages/EventDetail.d.ts.map +1 -1
- package/dist/pages/EventDetail.js +2 -1
- package/dist/pages/EventDetail.js.map +1 -1
- package/dist/pages/FormsPage.js +4 -4
- package/dist/pages/FormsPage.js.map +1 -1
- package/dist/pages/OpportunityDetail.d.ts.map +1 -1
- package/dist/pages/OpportunityDetail.js +2 -1
- package/dist/pages/OpportunityDetail.js.map +1 -1
- package/dist/pages/PartnershipDetail.d.ts.map +1 -1
- package/dist/pages/PartnershipDetail.js +2 -1
- package/dist/pages/PartnershipDetail.js.map +1 -1
- package/dist/pages/PeoplePage.d.ts.map +1 -1
- package/dist/pages/PeoplePage.js +7 -1
- package/dist/pages/PeoplePage.js.map +1 -1
- package/dist/pages/PersonDetail.d.ts.map +1 -1
- package/dist/pages/PersonDetail.js +2 -1
- package/dist/pages/PersonDetail.js.map +1 -1
- package/dist/pages/RaiseDetail.d.ts.map +1 -1
- package/dist/pages/RaiseDetail.js +2 -1
- package/dist/pages/RaiseDetail.js.map +1 -1
- package/dist/pages/SearchPage.d.ts.map +1 -1
- package/dist/pages/SearchPage.js +26 -14
- package/dist/pages/SearchPage.js.map +1 -1
- package/dist/pages/admin/AiAdminPage.d.ts +2 -0
- package/dist/pages/admin/AiAdminPage.d.ts.map +1 -0
- package/dist/pages/admin/AiAdminPage.js +19 -0
- package/dist/pages/admin/AiAdminPage.js.map +1 -0
- package/dist/pages/admin/AiPage.d.ts +5 -0
- package/dist/pages/admin/AiPage.d.ts.map +1 -0
- package/dist/pages/admin/AiPage.js +196 -0
- package/dist/pages/admin/AiPage.js.map +1 -0
- package/dist/pages/admin/McpPage.js +2 -2
- package/dist/pages/admin/McpPage.js.map +1 -1
- package/dist/pages/admin/McpPanel.d.ts +3 -0
- package/dist/pages/admin/McpPanel.d.ts.map +1 -0
- package/dist/pages/admin/McpPanel.js +123 -0
- package/dist/pages/admin/McpPanel.js.map +1 -0
- package/dist/pages/admin/PrivacyPage.js +19 -3
- package/dist/pages/admin/PrivacyPage.js.map +1 -1
- package/dist/pages/admin/WorkspaceSettingsPage.d.ts +5 -6
- package/dist/pages/admin/WorkspaceSettingsPage.d.ts.map +1 -1
- package/dist/pages/admin/WorkspaceSettingsPage.js +12 -17
- package/dist/pages/admin/WorkspaceSettingsPage.js.map +1 -1
- package/dist/pages/dashboard/attention.d.ts +0 -5
- package/dist/pages/dashboard/attention.d.ts.map +1 -1
- package/dist/pages/dashboard/attention.js +1 -21
- package/dist/pages/dashboard/attention.js.map +1 -1
- package/dist/pages/dashboard/sections.d.ts.map +1 -1
- package/dist/pages/dashboard/sections.js +6 -3
- package/dist/pages/dashboard/sections.js.map +1 -1
- package/dist/pages/forms/EmbedPanel.js +5 -2
- package/dist/pages/forms/EmbedPanel.js.map +1 -1
- package/dist/pages/forms/FieldsEditor.d.ts.map +1 -1
- package/dist/pages/forms/FieldsEditor.js +90 -8
- package/dist/pages/forms/FieldsEditor.js.map +1 -1
- package/dist/pages/forms/FormSettings.d.ts.map +1 -1
- package/dist/pages/forms/FormSettings.js +73 -6
- package/dist/pages/forms/FormSettings.js.map +1 -1
- package/dist/pages/forms/SubmissionDetail.d.ts.map +1 -1
- package/dist/pages/forms/SubmissionDetail.js +2 -1
- package/dist/pages/forms/SubmissionDetail.js.map +1 -1
- package/dist/pages/forms/SubmissionsTable.d.ts.map +1 -1
- package/dist/pages/forms/SubmissionsTable.js +2 -1
- package/dist/pages/forms/SubmissionsTable.js.map +1 -1
- package/dist/pages/forms/fieldList.d.ts +2 -2
- package/dist/pages/forms/fieldList.d.ts.map +1 -1
- package/dist/pages/forms/fieldList.js +38 -10
- package/dist/pages/forms/fieldList.js.map +1 -1
- package/dist/pages/forms/template.d.ts.map +1 -1
- package/dist/pages/forms/template.js +13 -0
- package/dist/pages/forms/template.js.map +1 -1
- package/dist/pages/handbook/HandbookPage.d.ts.map +1 -1
- package/dist/pages/handbook/HandbookPage.js +2 -3
- package/dist/pages/handbook/HandbookPage.js.map +1 -1
- package/dist/pages/onboarding/WorkspaceStepPage.d.ts.map +1 -1
- package/dist/pages/onboarding/WorkspaceStepPage.js +9 -27
- package/dist/pages/onboarding/WorkspaceStepPage.js.map +1 -1
- package/dist/pages/onboarding/onboardingRerun.d.ts +9 -0
- package/dist/pages/onboarding/onboardingRerun.d.ts.map +1 -1
- package/dist/pages/onboarding/onboardingRerun.js +11 -0
- package/dist/pages/onboarding/onboardingRerun.js.map +1 -1
- package/dist/registry/context.d.ts +7 -1
- package/dist/registry/context.d.ts.map +1 -1
- package/dist/registry/context.js +12 -0
- package/dist/registry/context.js.map +1 -1
- package/dist/registry/contributions.d.ts +70 -1
- package/dist/registry/contributions.d.ts.map +1 -1
- package/dist/registry/registry.d.ts +12 -1
- package/dist/registry/registry.d.ts.map +1 -1
- package/dist/registry/registry.js +21 -0
- package/dist/registry/registry.js.map +1 -1
- package/package.json +2 -2
- package/src/api/client.ts +1 -1
- package/src/api/resources/ai.ts +120 -0
- package/src/api/resources/conversions.ts +1 -1
- package/src/api/resources/forms.ts +56 -0
- package/src/api/resources/personIntake.ts +95 -0
- package/src/api/resources/session.ts +3 -1
- package/src/api/resources/workspace.ts +4 -4
- package/src/app/KelpieApp.tsx +4 -2
- package/src/components/ActivitiesPanel.tsx +39 -10
- package/src/components/ActivityAction.tsx +33 -0
- package/src/components/AgentTasks.tsx +138 -10
- package/src/components/DecisionsPanel.tsx +9 -1
- package/src/components/LinkedText.tsx +51 -0
- package/src/components/MarkdownView.tsx +37 -7
- package/src/components/NotesPanel.tsx +18 -12
- package/src/components/PersonIntakeDialog.tsx +226 -0
- package/src/components/PlanPanel.tsx +9 -1
- package/src/components/RecordLinkTextarea.tsx +257 -0
- package/src/components/Shell.tsx +1 -1
- package/src/components/WorkspaceSwitcher.tsx +15 -1
- package/src/index.ts +16 -0
- package/src/lib/fragmentTarget.ts +96 -0
- package/src/lib/recordLinks.ts +57 -0
- package/src/lib/recordReferences.ts +139 -0
- package/src/modules/PersonIntakeWizard.tsx +633 -0
- package/src/modules/ai.tsx +27 -0
- package/src/modules/aiRunner.ts +83 -0
- package/src/modules/personIntakeSelection.ts +24 -0
- package/src/pages/CompanyDetail.tsx +2 -1
- package/src/pages/DealDetail.tsx +2 -1
- package/src/pages/EnquiryDetail.tsx +2 -1
- package/src/pages/EventDetail.tsx +2 -1
- package/src/pages/FormsPage.tsx +4 -4
- package/src/pages/OpportunityDetail.tsx +2 -1
- package/src/pages/PartnershipDetail.tsx +2 -1
- package/src/pages/PeoplePage.tsx +21 -5
- package/src/pages/PersonDetail.tsx +2 -1
- package/src/pages/RaiseDetail.tsx +2 -1
- package/src/pages/SearchPage.tsx +30 -15
- package/src/pages/admin/AiAdminPage.tsx +52 -0
- package/src/pages/admin/AiPage.tsx +457 -0
- package/src/pages/admin/{McpPage.tsx → McpPanel.tsx} +15 -11
- package/src/pages/admin/PrivacyPage.tsx +42 -0
- package/src/pages/admin/WorkspaceSettingsPage.tsx +13 -28
- package/src/pages/dashboard/attention.ts +1 -24
- package/src/pages/dashboard/sections.tsx +9 -4
- package/src/pages/forms/EmbedPanel.tsx +5 -1
- package/src/pages/forms/FieldsEditor.tsx +204 -15
- package/src/pages/forms/FormSettings.tsx +156 -9
- package/src/pages/forms/SubmissionDetail.tsx +2 -1
- package/src/pages/forms/SubmissionsTable.tsx +2 -1
- package/src/pages/forms/fieldList.ts +39 -10
- package/src/pages/forms/template.ts +13 -0
- package/src/pages/handbook/HandbookPage.tsx +5 -6
- package/src/pages/onboarding/WorkspaceStepPage.tsx +19 -43
- package/src/pages/onboarding/onboardingRerun.ts +14 -0
- package/src/registry/context.ts +19 -0
- package/src/registry/contributions.ts +64 -1
- package/src/registry/registry.ts +48 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { RecordReference, RecordReferenceType, RecordTargetType } from '@kelpie/schemas'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Where each kind of record lives. A Candidate has no page of its own: it is
|
|
5
|
+
* reached through its Role. An Attendance is reached through its Event. A Note,
|
|
6
|
+
* Decision or Plan item is reached on the page of the record it is on; see
|
|
7
|
+
* `attachedHref`.
|
|
8
|
+
*/
|
|
9
|
+
const ROUTES: Readonly<Record<RecordReferenceType, string | undefined>> = {
|
|
10
|
+
person: '/people',
|
|
11
|
+
company: '/companies',
|
|
12
|
+
deal: '/deals',
|
|
13
|
+
opportunity: '/opportunities',
|
|
14
|
+
partnership: '/partnerships',
|
|
15
|
+
raise: '/fundraising',
|
|
16
|
+
enquiry: '/enquiries',
|
|
17
|
+
candidate: undefined,
|
|
18
|
+
event: '/events',
|
|
19
|
+
attendance: undefined,
|
|
20
|
+
role: '/hiring',
|
|
21
|
+
handbook_page: '/handbook',
|
|
22
|
+
list: '/lists',
|
|
23
|
+
form: '/forms',
|
|
24
|
+
note: undefined,
|
|
25
|
+
decision: undefined,
|
|
26
|
+
plan_item: undefined,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @returns The record's page, or undefined when it has none. A row without a
|
|
31
|
+
* link renders as plain text rather than as a link to nowhere.
|
|
32
|
+
*/
|
|
33
|
+
export function targetHref(targetType: RecordReferenceType, targetId: string): string | undefined {
|
|
34
|
+
const route = ROUTES[targetType]
|
|
35
|
+
|
|
36
|
+
return route === undefined ? undefined : `${route}/${targetId}`
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A Note, Decision or Plan item: the page of the record it is on, with its own
|
|
41
|
+
* id as the fragment, which the panel on that page scrolls to and highlights.
|
|
42
|
+
*/
|
|
43
|
+
export function attachedHref(
|
|
44
|
+
id: string,
|
|
45
|
+
parent: { readonly type: RecordTargetType; readonly id: string },
|
|
46
|
+
): string | undefined {
|
|
47
|
+
const page = targetHref(parent.type, parent.id)
|
|
48
|
+
|
|
49
|
+
return page === undefined ? undefined : `${page}#${id}`
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Where a cited record opens, or undefined when it has nowhere to open. */
|
|
53
|
+
export function referenceHref(reference: RecordReference): string | undefined {
|
|
54
|
+
return reference.parent === null
|
|
55
|
+
? targetHref(reference.targetType, reference.targetId)
|
|
56
|
+
: attachedHref(reference.targetId, reference.parent)
|
|
57
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { splitRecordLinkTokens } from '@kelpie/schemas'
|
|
2
|
+
import type { RecordLinkToken, RecordReference } from '@kelpie/schemas'
|
|
3
|
+
import type { Link, Parent, PhrasingContent, Root, Text } from 'mdast'
|
|
4
|
+
|
|
5
|
+
import { referenceHref } from './recordLinks.ts'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Turning the record ids a text cites into something a person can follow.
|
|
9
|
+
*
|
|
10
|
+
* The server already found the ids and named them (`references` on an activity
|
|
11
|
+
* or a note). This side only finds each named id in the text by string match.
|
|
12
|
+
* An id the server did not name stays as it is: it is not a record in this
|
|
13
|
+
* workspace, and a link to it would go nowhere.
|
|
14
|
+
*
|
|
15
|
+
* Text cites a record in two ways, and one pass reads both. A `[[type:id|Label]]`
|
|
16
|
+
* link token is read first, so the id inside it is never matched a second time as
|
|
17
|
+
* a bare id. The text between tokens is then searched for bare ids. A token the
|
|
18
|
+
* server named shows the record's current name; one it did not name shows the
|
|
19
|
+
* token's label as plain text, because the record is gone.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export type TextSegment =
|
|
23
|
+
| { readonly kind: 'text'; readonly text: string }
|
|
24
|
+
| { readonly kind: 'reference'; readonly reference: RecordReference }
|
|
25
|
+
| { readonly kind: 'unresolved'; readonly token: RecordLinkToken }
|
|
26
|
+
|
|
27
|
+
/** What an unresolved token shows: its label, or its id when it has none. */
|
|
28
|
+
export function unresolvedLabel(token: RecordLinkToken): string {
|
|
29
|
+
return token.label ?? token.targetId
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function escapeForPattern(value: string): string {
|
|
33
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* `text` cut into plain runs and cited records, in order.
|
|
38
|
+
*
|
|
39
|
+
* @returns One text segment holding all of `text` when nothing is cited.
|
|
40
|
+
*/
|
|
41
|
+
export function splitOnReferences(
|
|
42
|
+
text: string,
|
|
43
|
+
references: readonly RecordReference[],
|
|
44
|
+
): readonly TextSegment[] {
|
|
45
|
+
const byTokenId = new Map(references.map((reference) => [reference.targetId, reference]))
|
|
46
|
+
|
|
47
|
+
return splitRecordLinkTokens(text).flatMap((segment): readonly TextSegment[] => {
|
|
48
|
+
if (segment.kind === 'text') {
|
|
49
|
+
return splitOnBareIds(segment.text, references)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const reference = byTokenId.get(segment.token.targetId)
|
|
53
|
+
|
|
54
|
+
return reference === undefined
|
|
55
|
+
? [{ kind: 'unresolved', token: segment.token }]
|
|
56
|
+
: [{ kind: 'reference', reference }]
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Bare ids only. Every segment it returns is `text` or `reference`. */
|
|
61
|
+
function splitOnBareIds(text: string, references: readonly RecordReference[]): TextSegment[] {
|
|
62
|
+
if (references.length === 0) {
|
|
63
|
+
return text.length === 0 ? [] : [{ kind: 'text', text }]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const byId = new Map(references.map((reference) => [reference.targetId, reference]))
|
|
67
|
+
// Longest first, so an id can never lose to a shorter one it starts with.
|
|
68
|
+
const ids = [...byId.keys()].sort((left, right) => right.length - left.length)
|
|
69
|
+
const pattern = new RegExp(`(${ids.map(escapeForPattern).join('|')})`)
|
|
70
|
+
const segments: TextSegment[] = []
|
|
71
|
+
|
|
72
|
+
// `split` with one capture group puts each match at an odd index.
|
|
73
|
+
text.split(pattern).forEach((part, index) => {
|
|
74
|
+
const reference = index % 2 === 1 ? byId.get(part) : undefined
|
|
75
|
+
|
|
76
|
+
if (reference !== undefined) {
|
|
77
|
+
segments.push({ kind: 'reference', reference })
|
|
78
|
+
} else if (part.length > 0) {
|
|
79
|
+
segments.push({ kind: 'text', text: part })
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
return segments
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A cited record as a Markdown node: a link to its page, or its name when it has none. */
|
|
87
|
+
function referenceNode(reference: RecordReference): Link | Text {
|
|
88
|
+
const href = referenceHref(reference)
|
|
89
|
+
const name: Text = { type: 'text', value: reference.name }
|
|
90
|
+
|
|
91
|
+
return href === undefined ? name : { type: 'link', url: href, title: null, children: [name] }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Node types whose text must stay as written: a link cannot hold a link, and code is literal. */
|
|
95
|
+
const LEAVE_ALONE = new Set(['link', 'linkReference', 'code', 'inlineCode'])
|
|
96
|
+
|
|
97
|
+
function isParent(node: { readonly type: string }): node is Parent {
|
|
98
|
+
return 'children' in node && Array.isArray(node.children)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function rewriteChildren(parent: Parent, references: readonly RecordReference[]): void {
|
|
102
|
+
const rewritten: Parent['children'] = []
|
|
103
|
+
|
|
104
|
+
for (const child of parent.children) {
|
|
105
|
+
if (child.type === 'text') {
|
|
106
|
+
for (const segment of splitOnReferences(child.value, references)) {
|
|
107
|
+
rewritten.push(
|
|
108
|
+
segment.kind === 'text'
|
|
109
|
+
? ({ type: 'text', value: segment.text } satisfies PhrasingContent)
|
|
110
|
+
: segment.kind === 'reference'
|
|
111
|
+
? referenceNode(segment.reference)
|
|
112
|
+
: ({ type: 'text', value: unresolvedLabel(segment.token) } satisfies PhrasingContent),
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
if (isParent(child) && !LEAVE_ALONE.has(child.type)) {
|
|
117
|
+
rewriteChildren(child, references)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
rewritten.push(child)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
parent.children = rewritten
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A remark plugin: every cited record in the Markdown's text, bare id or link
|
|
129
|
+
* token, becomes a link to the record, labelled with its name. The source string
|
|
130
|
+
* is not touched, so what the author wrote is still what is stored and edited.
|
|
131
|
+
*/
|
|
132
|
+
export function remarkRecordReferences(
|
|
133
|
+
references: readonly RecordReference[],
|
|
134
|
+
): () => (tree: Root) => void {
|
|
135
|
+
// Runs even with no references: a token must still lose its brackets.
|
|
136
|
+
return () => (tree) => {
|
|
137
|
+
rewriteChildren(tree, references)
|
|
138
|
+
}
|
|
139
|
+
}
|