Skip to content

Commit

Permalink
feat: make the markdownish parser look nice
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarpl committed Dec 8, 2023
1 parent e337c36 commit 2007e03
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 150 deletions.
14 changes: 9 additions & 5 deletions packages/apps/client/src/ScriptEditor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import { formatingKeymap } from './keymaps'
import { deselectAll } from './commands/deselectAll'
import { fromMarkdown } from '../lib/prosemirrorDoc'

const LOREM_IPSUM =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent sollicitudin ipsum at lacinia sodales. *Sed* in **pharetra _mauris_**, id facilisis nibh. Curabitur eget erat bibendum, aliquam ligula ac, interdum orci. Curabitur non mollis nibh. Pellentesque ultrices suscipit diam ac fermentum. Morbi id velit consectetur, auctor ligula scelerisque, vulputate ante. Nunc mattis consectetur eleifend. Aenean vestibulum porta mollis. Cras ultrices facilisis turpis, et vulputate felis tempor at. Aliquam ultricies commodo odio at vehicula. Curabitur lobortis lectus at lacus commodo tincidunt. Donec vulputate urna efficitur, vehicula urna vel, porttitor urna.\n' +
'Duis congue molestie neque, et sollicitudin lacus porta eget. Etiam massa dui, cursus vitae lacus sit **amet**, aliquet bibendum elit. Morbi tincidunt quis metus ut luctus. Proin tincidunt suscipit vestibulum. In eu cursus quam. Praesent lacus mauris, euismod nec lacus in, tincidunt ultrices justo. Sed ac rhoncus quam. Praesent libero elit, convallis ut urna nec, interdum elementum diam. Pellentesque aliquet, mi vitae faucibus euismod, mauris lorem auctor felis, tincidunt bibendum erat nisl in nisi.\n' +
'Donec ac rhoncus ex. Pellentesque eleifend ante id maximus *mollis*. Duis in mauris vel ligula venenatis gravida.\n\n\\*Mauris blandit arcu a lorem cursus ornare. Vestibulum at ligula vel nisi eleifend pretium. Vivamus et nunc scelerisque, suscipit dolor nec, ornare elit. Nam ut tristique est. Suspendisse sollicitudin tortor quam, eget cursus quam porttitor nec. Fusce convallis libero massa, a consequat tortor accumsan id. Pellentesque at diam sit amet tortor suscipit bibendum sed et elit. Etiam ac tellus tellus. Cras pulvinar sem et augue consequat mattis. \n' +
'Duis mollis ut enim vitae lobortis. ~Nulla mi libero~, blandit sit amet congue eu, vehicula vel sem. Donec maximus lacus \\~ac nisi blandit sodales. Fusce sed lectus iaculis, tempus quam lacinia, gravida velit. In imperdiet, sem sit amet commodo eleifend, turpis tellus lobortis metus, et rutrum mi sapien vel nisl. Pellentesque at est non tortor efficitur tincidunt vitae in ex. In gravida pulvinar ligula eget pellentesque. Nullam viverra orci velit, at dictum diam imperdiet sit amet. Morbi consequat est vitae mi consequat fringilla. Phasellus pharetra turpis nulla, at molestie nunc hendrerit ut. \n' +
'Aenean ut nulla ut diam imperdiet laoreet sed sed enim. **Vivamus bibendum** tempus metus ac consectetur. Aliquam ut nisl sed mauris sodales dignissim. Integer consectetur sapien quam, sit amet blandit quam cursus ac. Quisque vel convallis erat. Aliquam ac interdum nisi. Praesent id sapien vitae sem venenatis sollicitudin. '

export function Editor({
initialValue,
className,
Expand Down Expand Up @@ -56,7 +63,7 @@ export function Editor({
[
schema.node(schema.nodes.lineTitle, undefined, [schema.text('Line title')]),
...fromMarkdown(
'Raz _dwa **trzy**_. ~Cztery.~\n\nPięć _sześć_ siedem.\nRaz\n\n\n\n\n Some more ~Markdown **Here**~'
'Raz _dwa **trzy**_. ~Cztery.~\n\nPięć _sześć_ siedem.\nRaz\n\n\n\nSome more ~Markdown **Here**~'
),
]
),
Expand All @@ -65,10 +72,7 @@ export function Editor({
{
lineId: randomId(),
},
[
schema.node(schema.nodes.lineTitle, undefined, schema.text('Line title')),
schema.node(schema.nodes.paragraph, undefined, schema.text('Script...')),
]
[schema.node(schema.nodes.lineTitle, undefined, schema.text('Line title')), ...fromMarkdown(LOREM_IPSUM)]
),
schema.node(
schema.nodes.line,
Expand Down
144 changes: 0 additions & 144 deletions packages/apps/client/src/lib/markdownishParser.ts

This file was deleted.

37 changes: 37 additions & 0 deletions packages/apps/client/src/lib/mdParser/astNodes.ts
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
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 packages/apps/client/src/lib/mdParser/constructs/escape.ts
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 packages/apps/client/src/lib/mdParser/constructs/paragraph.ts
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 packages/apps/client/src/lib/mdParser/constructs/reverse.ts
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,
},
}
}
Loading

0 comments on commit 2007e03

Please sign in to comment.