-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
96 additions
and
17 deletions.
There are no files selected for viewing
Binary file not shown.
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 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 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 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 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 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 |
---|---|---|
@@ -1,25 +1,78 @@ | ||
import { fromMarkdown as mdastFromMarkdown } from 'mdast-util-from-markdown' | ||
import type { Node, Parent } from 'mdast' | ||
import { fromMarkdown as mdAstFromMarkdown } from 'mdast-util-from-markdown' | ||
import { toMarkdown as mdAstToMarkdown } from 'mdast-util-to-markdown' | ||
import type { Node as MdAstNode, Parent as MdAstParent, Literal as MdAstLiteral } from 'mdast' | ||
import { Node as ProsemirrorNode } from 'prosemirror-model' | ||
import { directive } from 'micromark-extension-directive' | ||
import { directiveFromMarkdown } from 'mdast-util-directive' | ||
import { schema } from '../ScriptEditor/scriptSchema' | ||
import { directiveFromMarkdown, directiveToMarkdown } from 'mdast-util-directive' | ||
|
||
export function fromMarkdown(text: string): void { | ||
const ast = mdastFromMarkdown(text, 'utf-8', { | ||
export function fromMarkdown(text: string): ProsemirrorNode[] { | ||
const ast = mdAstFromMarkdown(text, 'utf-8', { | ||
extensions: [directive()], | ||
mdastExtensions: [directiveFromMarkdown()], | ||
}) | ||
|
||
traverseNodes(ast.children) | ||
return traverseMdAstNodes(ast.children) | ||
} | ||
|
||
function traverseNodes(nodes: Node[]) { | ||
export function toMarkdown(doc: ProsemirrorNode[]): string { | ||
return mdAstToMarkdown( | ||
{ | ||
type: 'root', | ||
children: [], | ||
}, | ||
{ | ||
extensions: [directiveToMarkdown()], | ||
} | ||
) | ||
} | ||
|
||
function mdastToEditorSchemaNode(node: MdAstNode, children?: ProsemirrorNode[]): ProsemirrorNode[] { | ||
console.log(node) | ||
if (node.type === 'paragraph') { | ||
return [schema.node(schema.nodes.paragraph, undefined, children)] | ||
} else if (node.type === 'text' && isLiteral(node)) { | ||
return [schema.text(node.value)] | ||
} else if (node.type === 'strong' && children) { | ||
return children.map((child) => child.mark([...child.marks, schema.mark(schema.marks.bold)])) | ||
} else if (node.type === 'emphasis' && children) { | ||
return children.map((child) => child.mark([...child.marks, schema.mark(schema.marks.italic)])) | ||
} else if (isTextDirective(node) && node.name === 'reverse' && children) { | ||
return children.map((child) => child.mark([...child.marks, schema.mark(schema.marks.reverse)])) | ||
} else { | ||
return [schema.text('[UNKNOWN]')] | ||
} | ||
} | ||
|
||
function traverseMdAstNodes(nodes: MdAstNode[]): ProsemirrorNode[] { | ||
const result: ProsemirrorNode[] = [] | ||
for (const childNode of nodes) { | ||
console.dir(childNode) | ||
if (isParent(childNode)) traverseNodes(childNode.children) | ||
let children: ProsemirrorNode[] = [] | ||
if (isParent(childNode)) { | ||
children = traverseMdAstNodes(childNode.children) | ||
} | ||
result.push(...mdastToEditorSchemaNode(childNode, children)) | ||
} | ||
|
||
return result | ||
} | ||
|
||
function isParent(node: Node): node is Parent { | ||
function isParent(node: MdAstNode): node is MdAstParent { | ||
if ('children' in node && Array.isArray(node.children)) return true | ||
return false | ||
} | ||
|
||
function isLiteral(node: MdAstNode): node is MdAstLiteral { | ||
if ('value' in node) return true | ||
return false | ||
} | ||
|
||
function isTextDirective(node: MdAstNode): node is MdAstTextDirective { | ||
if (node.type === 'textDirective' && 'name' in node) return true | ||
return false | ||
} | ||
|
||
interface MdAstTextDirective extends MdAstNode { | ||
name: string | ||
attributes: Record<string, string> | ||
} |
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