-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make the markdownish parser look nice
- Loading branch information
Showing
9 changed files
with
309 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
interface NodeBase { | ||
type: string | ||
} | ||
|
||
export interface ParentNodeBase extends NodeBase { | ||
children: Node[] | ||
} | ||
|
||
export interface RootNode extends ParentNodeBase { | ||
type: 'root' | ||
} | ||
|
||
export interface ParagraphNode extends ParentNodeBase { | ||
type: 'paragraph' | ||
} | ||
|
||
export interface TextNode extends NodeBase { | ||
type: 'text' | ||
value: string | ||
} | ||
|
||
export interface StrongNode extends ParentNodeBase { | ||
type: 'strong' | ||
code: string | ||
} | ||
|
||
export interface EmphasisNode extends ParentNodeBase { | ||
type: 'emphasis' | ||
code: string | ||
} | ||
|
||
export interface ReverseNode extends ParentNodeBase { | ||
type: 'reverse' | ||
code: string | ||
} | ||
|
||
export type Node = RootNode | ParagraphNode | TextNode | StrongNode | EmphasisNode | ReverseNode |
45 changes: 45 additions & 0 deletions
45
packages/apps/client/src/lib/mdParser/constructs/emphasisAndStrong.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { NodeConstruct, ParserState } from '..' | ||
import { EmphasisNode, StrongNode } from '../astNodes' | ||
|
||
export function emphasisAndStrong(): NodeConstruct { | ||
function emphasisOrStrong(char: string, state: ParserState) { | ||
if (state.nodeCursor === null) throw new Error('cursor === null assertion') | ||
if ((state.nodeCursor.type === 'emphasis' || state.nodeCursor.type === 'strong') && 'code' in state.nodeCursor) { | ||
if (state.nodeCursor.code === char) { | ||
if (state.peek() === char) { | ||
state.consume() | ||
} | ||
|
||
state.flushBuffer() | ||
state.popNode() | ||
return false | ||
} | ||
} | ||
|
||
state.flushBuffer() | ||
|
||
let type: 'emphasis' | 'strong' = 'emphasis' | ||
|
||
if (state.peek() === char) { | ||
type = 'strong' | ||
state.consume() | ||
} | ||
|
||
const emphasisOrStrongNode: EmphasisNode | StrongNode = { | ||
type, | ||
children: [], | ||
code: char, | ||
} | ||
state.pushNode(emphasisOrStrongNode) | ||
|
||
return false | ||
} | ||
|
||
return { | ||
name: 'emphasisOrStrong', | ||
char: { | ||
'*': emphasisOrStrong, | ||
_: emphasisOrStrong, | ||
}, | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/apps/client/src/lib/mdParser/constructs/escape.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NodeConstruct, ParserState } from '..' | ||
|
||
export function escape(): NodeConstruct { | ||
function escapeChar(_: string, state: ParserState) { | ||
state.dataStore['inEscape'] = true | ||
} | ||
|
||
function passthroughChar(_: string, state: ParserState) { | ||
if (state.dataStore['inEscape'] !== true) return | ||
state.dataStore['inEscape'] = false | ||
return true | ||
} | ||
|
||
return { | ||
name: 'escape', | ||
char: { | ||
'\\': escapeChar, | ||
any: passthroughChar, | ||
}, | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/apps/client/src/lib/mdParser/constructs/paragraph.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { NodeConstruct, ParserState } from '..' | ||
import { ParagraphNode } from '../astNodes' | ||
|
||
export function paragraph(): NodeConstruct { | ||
function paragraphStart(_: string, state: ParserState) { | ||
if (state.nodeCursor !== null) return | ||
const newParagraph: ParagraphNode = { | ||
type: 'paragraph', | ||
children: [], | ||
} | ||
state.replaceStack(newParagraph) | ||
} | ||
|
||
function paragraphEnd(char: string, state: ParserState) { | ||
if (state.nodeCursor === null) { | ||
paragraphStart(char, state) | ||
} | ||
|
||
state.flushBuffer() | ||
state.nodeCursor = null | ||
|
||
return false | ||
} | ||
|
||
return { | ||
name: 'paragraph', | ||
char: { | ||
end: paragraphEnd, | ||
'\n': paragraphEnd, | ||
any: paragraphStart, | ||
}, | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/apps/client/src/lib/mdParser/constructs/reverse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { NodeConstruct, ParserState } from '..' | ||
import { ReverseNode } from '../astNodes' | ||
|
||
export function reverse(): NodeConstruct { | ||
function reverse(char: string, state: ParserState) { | ||
if (state.nodeCursor === null) throw new Error('cursor === null assertion') | ||
if (state.nodeCursor.type === 'reverse' && 'code' in state.nodeCursor) { | ||
if (state.nodeCursor.code === char) { | ||
state.flushBuffer() | ||
state.popNode() | ||
return false | ||
} | ||
} | ||
|
||
state.flushBuffer() | ||
|
||
const type = 'reverse' | ||
|
||
const reverseNode: ReverseNode = { | ||
type, | ||
children: [], | ||
code: char, | ||
} | ||
state.pushNode(reverseNode) | ||
|
||
return false | ||
} | ||
|
||
return { | ||
name: 'reverse', | ||
char: { | ||
'~': reverse, | ||
}, | ||
} | ||
} |
Oops, something went wrong.