Skip to content

Commit 8af3941

Browse files
authored
Merge pull request #208 from orgapp/editor
tidy up orga types
2 parents f185a31 + b0ec58b commit 8af3941

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

packages/orga/src/parse/context.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import assert from 'assert'
2-
import type { Node, Point } from 'unist'
1+
import type { Node, Point, Parent } from 'unist'
32
import { not, test } from './index.js'
43
import type { Predicate } from './index.js'
54
import type { Lexer } from '../tokenize/index.js'
6-
import { Attributes, Document, isSection, Parent } from '../types.js'
5+
import { Attributes, Document, isSection } from '../types.js'
76

87
interface Snapshot {
98
stack: Parent[]
@@ -73,7 +72,9 @@ export function createContext(lexer: Lexer): Context {
7372
lexer.peek(-1)?.position?.end || { line: 1, column: 1, offset: 0 }
7473
node.position.end = point(end)
7574

76-
assert(node, 'unexpected empty stack')
75+
if (!node) {
76+
throw new Error('unexpected empty stack')
77+
}
7778
// attach to tree
7879
if (stack.length > 0) {
7980
push(node)
@@ -87,14 +88,15 @@ export function createContext(lexer: Lexer): Context {
8788
if (test(last, predicate)) {
8889
return pop()
8990
}
90-
assert(
91-
!strict,
92-
`
91+
if (strict) {
92+
throw new Error(
93+
`
9394
can not strictly exit ${predicate},
9495
actual: ${last.type}
9596
location: line: ${last.position.start.line}, column: ${last.position.start.column}
9697
`.trim()
97-
)
98+
)
99+
}
98100
}
99101

100102
const exitTo = (predicate: Predicate) => {
@@ -121,7 +123,9 @@ location: line: ${last.position.start.line}, column: ${last.position.start.colum
121123

122124
const push = (node: Node) => {
123125
if (!node) return
124-
assert(stack.length > 0, 'unexpected empty stack')
126+
if (stack.length === 0) {
127+
throw new Error('unexpected empty stack')
128+
}
125129
const parent = stack[stack.length - 1]
126130
parent.children.push(node)
127131
// node.parent = parent

packages/orga/src/parse/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import assert from 'assert'
21
import { Lexer } from '../tokenize'
32
import { Node } from 'unist'
43
import { Document, Parent, Token } from '../types.js'
@@ -114,7 +113,9 @@ export const parse = (lexer: Lexer): Document => {
114113

115114
outter: while (handler()) {
116115
// prevent infinit loop
117-
assert(maxStaleIterations > 0, `it's stuck. \n${context.state}`)
116+
if (maxStaleIterations === 0) {
117+
throw new Error(`it's stuck. \n${context.state}`)
118+
}
118119

119120
let nothingMatches = true
120121

@@ -159,7 +160,9 @@ export const parse = (lexer: Lexer): Document => {
159160
maxStaleIterations = 10
160161
}
161162
}
162-
assert(!lexer.peek(), `not all tokens processed`)
163+
if (lexer.peek()) {
164+
throw new Error(`not all tokens processed`)
165+
}
163166

164167
context.exitTo('document')
165168
context.exit('document')

packages/orga/src/parse/paragraph.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Action } from './index.js'
2-
import { Opening, Token } from '../types.js'
2+
import { Opening, isParagraph, PhrasingContent } from '../types.js'
33
import { clone, isPhrasingContent } from '../utils.js'
44
import { Context } from './context.js'
55
import phrasingContent from './phrasing.js'
66
import { isImage } from './_utils.js'
77

8-
const isWhitespaces = (node: Token) => {
8+
const isWhitespaces = (node: PhrasingContent) => {
99
return (
1010
(node.type === 'text' && node.value.trim().length === 0) ||
1111
node.type === 'newline' ||
@@ -28,7 +28,7 @@ const paragraph: Action = () => {
2828

2929
const exitPragraph = (context: Context) => {
3030
const paragraph = context.getParent()
31-
if (paragraph.type !== 'paragraph') return
31+
if (!isParagraph(paragraph)) return
3232
if (
3333
paragraph.children.length === 0 ||
3434
paragraph.children.every(isWhitespaces)

packages/orga/src/types.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import type { Literal as UnistLiteral, Node, Parent } from 'unist'
1+
import type {
2+
Literal as UnistLiteral,
3+
Node,
4+
Parent as UnistParent,
5+
} from 'unist'
26

37
export interface Literal extends UnistLiteral {
48
value: string
59
}
610

7-
export { Parent }
11+
export interface Parent extends UnistParent {
12+
children: Content[]
13+
}
814

915
export type Primitive = string | number | boolean
1016

@@ -37,9 +43,8 @@ export interface Section extends Parent {
3743
children: Content[]
3844
}
3945

40-
type TopLevelContent = Content | Keyword | Footnote
41-
42-
type Content =
46+
// --- content types ----
47+
export type BlockContent =
4348
| Section
4449
| Paragraph
4550
| Block
@@ -52,6 +57,16 @@ type Content =
5257
| HTML
5358
| JSX
5459

60+
type TopLevelContent = BlockContent | Keyword | Footnote
61+
62+
export type Content =
63+
| TopLevelContent
64+
| TableContent
65+
| TableRow
66+
| TableCell
67+
| ListContent
68+
| PhrasingContent
69+
5570
export interface Footnote extends Parent {
5671
type: 'footnote'
5772
label: string
@@ -79,11 +94,13 @@ export interface Planning extends Node {
7994
timestamp: Timestamp
8095
}
8196

97+
type ListContent = ListItem | List
98+
8299
export interface List extends Parent, Attributed {
83100
type: 'list'
84101
indent: number
85102
ordered: boolean
86-
children: (List | ListItem)[]
103+
children: ListContent[]
87104
}
88105

89106
type TableContent = TableRow | TableRule
@@ -107,6 +124,7 @@ export interface ListItem extends Parent {
107124
type: 'list.item'
108125
indent: number
109126
tag?: string
127+
children: PhrasingContent[]
110128
}
111129

112130
export interface Headline extends Parent {
@@ -116,6 +134,7 @@ export interface Headline extends Parent {
116134
actionable: boolean
117135
priority?: string
118136
tags?: string[]
137+
children: PhrasingContent[]
119138
}
120139

121140
export interface Paragraph extends Parent, Attributed {
@@ -161,7 +180,14 @@ export type Token =
161180
| Closing
162181
| LinkPath
163182

164-
export type PhrasingContent = Text | Link | FootnoteReference | Newline
183+
export type PhrasingContent =
184+
| Text
185+
| Link
186+
| FootnoteReference
187+
| Newline
188+
| EmptyLine
189+
| HTML
190+
| JSX
165191

166192
export interface HorizontalRule extends Node {
167193
type: 'hr'
@@ -342,6 +368,10 @@ export function isSection(node: Node): node is Section {
342368
return node.type === 'section'
343369
}
344370

371+
export function isParagraph(node: Node): node is Paragraph {
372+
return node.type === 'paragraph'
373+
}
374+
345375
export function isLink(node: Node): node is Link {
346376
return node.type === 'link'
347377
}

0 commit comments

Comments
 (0)