-
Notifications
You must be signed in to change notification settings - Fork 2
/
language-extensions.ts
219 lines (209 loc) · 7.37 KB
/
language-extensions.ts
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
import { foldService, syntaxTree } from "@codemirror/language"
import { EditorState } from "@codemirror/state";
import { SyntaxNode } from "@lezer/common"
import { LRParser } from "@lezer/lr";
import { TOKEN } from 'codemirror-lang-orgmode';
export function nodeTypeClass(node_type_id: number): string {
if (node_type_id === TOKEN.TextBold) {
return "org-text-bold"
} else if (node_type_id === TOKEN.TextItalic) {
return "org-text-italic"
} else if (node_type_id === TOKEN.TextUnderline) {
return "org-text-underline"
} else if (node_type_id === TOKEN.TextVerbatim) {
return "org-text-verbatim"
} else if (node_type_id === TOKEN.TextCode) {
return "org-text-code"
} else if (node_type_id === TOKEN.TextStrikeThrough) {
return "org-text-strikethrough"
} else if (node_type_id === TOKEN.Block) {
return "org-block"
} else if (
node_type_id === TOKEN.RegularLink ||
node_type_id === TOKEN.AngleLink ||
node_type_id === TOKEN.PlainLink
) {
return "org-link"
} else if (node_type_id === TOKEN.Heading) {
return "org-heading"
} else if (node_type_id === TOKEN.Title) {
return "org-title"
} else if (
node_type_id === TOKEN.PlanningDeadline ||
node_type_id === TOKEN.PlanningScheduled ||
node_type_id === TOKEN.PlanningClosed
) {
return "org-planning"
} else if (node_type_id === TOKEN.PropertyDrawer) {
return "org-propertydrawer"
} else if (
node_type_id === TOKEN.ZerothSection ||
node_type_id === TOKEN.Section
) {
return "org-section"
} else if (
node_type_id === TOKEN.CommentLine ||
node_type_id === TOKEN.KeywordComment
) {
return "org-comment"
} else if (node_type_id === TOKEN.TodoKeyword) {
return "org-keyword"
} else if (node_type_id === TOKEN.Priority) {
return "org-priority"
} else if (node_type_id === TOKEN.Tags) {
return "org-tags"
}
throw Error("Not a markup node")
}
export const OrgFoldCompute = (state: EditorState, from: number, to: number) => {
let currentLineNode = syntaxTree(state).topNode.resolve(from, 1).node
const onFirstLine = (state.doc.lineAt(from).number === state.doc.lineAt(currentLineNode.from).number)
if (currentLineNode.type.id === TOKEN.Heading) {
const heading = currentLineNode
const hasSection = currentLineNode.getChild(TOKEN.Section)
const hasHeading = currentLineNode.getChild(TOKEN.Heading)
if (!hasSection && !hasHeading) {
return null
}
let block_to = heading.to
if (state.doc.sliceString(block_to-1, block_to) === '\n') {
block_to = block_to - 1
}
return { from: to, to: block_to };
} else if (currentLineNode.type.id === TOKEN.PropertyDrawer) {
if (!onFirstLine) {
return null
}
const propertyDrawer = currentLineNode
let block_to = propertyDrawer.to
if (state.doc.sliceString(block_to-1, block_to) === '\n') {
block_to = block_to - 1
}
return { from: to, to: block_to };
} else if (currentLineNode.type.id === TOKEN.Block) {
if (!onFirstLine) {
return null
}
const blockNode = currentLineNode
let block_to = blockNode.to
if (state.doc.sliceString(block_to-1, block_to) === '\n') {
block_to = block_to - 1
}
return { from: to, to: block_to };
}
return null
}
export const makeHeadingsFoldable = foldService.of(OrgFoldCompute);
function linkIsImage(linkText: string) {
if (!linkText.includes(".")) {
return false
}
const ext = linkText.slice(linkText.lastIndexOf("."))
const imageExtensions = ['.apng', '.avif', '.gif', '.jpeg', '.jpg', '.png', '.svg', '.webp']
return imageExtensions.includes(ext)
}
export type LinkHandler = "external" | "internal-file" | "internal-inline-image" | "internal-id"
function parseLinkText(linkText: string): [string, LinkHandler] {
const idx = linkText.indexOf(':')
let linkPath = null
let linkHandler: LinkHandler = null
let linkType = null
if (idx === -1) { // case 'PATHINNER'
linkHandler = "internal-file"
linkPath = linkText
} else if (/:\/\//.test(linkText)) { // case 'LINKTYPE://PATHINNER'
linkPath = linkText
linkHandler = "external"
} else { // case 'LINKTYPE:PATHINNER'
linkType = linkText.slice(0, idx)
linkPath = linkText.slice(idx+1)
if (linkType === 'file') {
linkHandler = "internal-file"
} else if (linkType === 'id') {
linkHandler = "internal-id"
} else {
// not handled
linkHandler = "internal-file"
}
}
return [linkPath, linkHandler]
}
export const extractLinkFromNode = (node: number, linkText: string): [string, string, LinkHandler, number] => {
let linkHandler: LinkHandler = null
let linkPath = null
let displayText = null
let hasLinkDescription = false
let displayTextFromOffset = null
let displayTextToOffset = null
if (node === TOKEN.PlainLink) {
linkPath = linkText
displayTextFromOffset = 0
displayTextToOffset = 0
linkHandler = "external"
let [linkPathDetected, linkHandlerDetected] = parseLinkText(linkText)
if (linkHandlerDetected == "internal-id") {
linkHandler = "internal-id"
linkPath = linkPathDetected
}
} else if (node === TOKEN.RegularLink) {
let innerLinkText
if (/\]\[/.test(linkText)) {
const idx = linkText.search(/\]\[/)
innerLinkText = linkText.slice(2, idx)
displayTextFromOffset = idx + 2
displayTextToOffset = -2
hasLinkDescription = true
} else {
innerLinkText = linkText.slice(2, -2)
displayTextFromOffset = 2
displayTextToOffset = -2
}
[linkPath, linkHandler] = parseLinkText(innerLinkText)
} else if (node === TOKEN.AngleLink) {
[linkPath, linkHandler] = parseLinkText(linkText.slice(1, -1))
displayTextFromOffset = 1
displayTextToOffset = -1
}
if (linkHandler === "internal-file" && linkIsImage(linkPath) && !hasLinkDescription) {
linkHandler = "internal-inline-image"
displayTextFromOffset = null
displayTextToOffset = null
}
if (displayTextFromOffset !== null) {
displayText = linkText.slice(displayTextFromOffset, linkText.length+displayTextToOffset)
}
return [linkPath, displayText, linkHandler, displayTextFromOffset]
}
function* iterateHeadings(node: SyntaxNode): Iterable<SyntaxNode> {
const headings = node.getChildren(TOKEN.Heading)
for (const heading of headings) {
yield heading
yield* iterateHeadings(heading)
}
}
export function* iterateOrgIds(orgmodeParser: LRParser, orgContent: string) {
const tree = orgmodeParser.parse(orgContent)
const id_regex = /:ID:\s+([^\s]+)\s*/ // TODO: to replace by a grammar token?
const topPropertyDrawer = tree.topNode.getChild(TOKEN.ZerothSection)?.getChild(TOKEN.PropertyDrawer)
if (topPropertyDrawer) {
const top_pd_content = orgContent.slice(topPropertyDrawer.from, topPropertyDrawer.to)
const match_file = id_regex.exec(top_pd_content)
if (match_file) {
const extracted_id = match_file[1]
yield {orgId: extracted_id, start: 0}
}
}
for (const heading of iterateHeadings(tree.topNode)) {
const propertyDrawer = heading.node.getChild(TOKEN.Section)?.getChild(TOKEN.PropertyDrawer)
if (!propertyDrawer) {
continue
}
const heading_start = heading.from
const pd_content = orgContent.slice(propertyDrawer.from, propertyDrawer.to)
const match_heading = id_regex.exec(pd_content)
if (match_heading) {
const extracted_id = match_heading[1]
yield {orgId: extracted_id, start: heading_start}
}
}
}