Skip to content

Commit

Permalink
fix: type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Jan 5, 2024
1 parent dc74d04 commit 65df146
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions src/exportMarkdownFromLexical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type { Options as ToMarkdownOptions } from 'mdast-util-to-markdown'
* This is part of the process that converts the editor contents to markdown.
* @group Markdown Processing
*/
export interface LexicalExportVisitor<LN extends LexicalNode, UN extends Mdast.Content> {
export interface LexicalExportVisitor<LN extends LexicalNode, UN extends Mdast.Nodes> {
/**
* Return true if the given node is of the type that this visitor can process.
* You can safely use the node type guard functions (as in $isParagraphNode, $isLinkNode, etc.) here.
Expand Down Expand Up @@ -66,13 +66,13 @@ export interface LexicalExportVisitor<LN extends LexicalNode, UN extends Mdast.C
* Return true if the current node should be joined with the previous node.
* This is necessary due to some inconsistencies between the lexical tree and the mdast tree when it comes to formatting.
*/
shouldJoin?(prevNode: Mdast.Content, currentNode: UN): boolean
shouldJoin?(prevNode: Mdast.RootContent, currentNode: UN): boolean

/**
* Join the current node with the previous node, returning the resulting new node
* For this to be called by the tree walk, shouldJoin must return true.
*/
join?<T extends Mdast.Content>(prevNode: T, currentNode: T): T
join?<T extends Mdast.RootContent>(prevNode: T, currentNode: T): T

/**
* Default 0, optional, sets the priority of the visitor. The higher the number, the earlier it will be called.
Expand All @@ -84,7 +84,7 @@ export interface LexicalExportVisitor<LN extends LexicalNode, UN extends Mdast.C
* A generic visitor that can be used to process any lexical node.
* @group Markdown Processing
*/
export type LexicalVisitor = LexicalExportVisitor<LexicalNode, Mdast.Content>
export type LexicalVisitor = LexicalExportVisitor<LexicalNode, Mdast.RootContent>

/**
* @internal
Expand Down Expand Up @@ -122,7 +122,7 @@ export function exportLexicalTreeToMdast({
referredComponents.add(componentName)
}

function appendToParent<T extends Mdast.Parent, C extends Mdast.Content>(parentNode: T, node: C): C | Mdast.Root {
function appendToParent<T extends Mdast.Parent, C extends Mdast.RootContent>(parentNode: T, node: C): C | Mdast.Root {
if (unistRoot === null) {
unistRoot = node as unknown as Mdast.Root
return unistRoot
Expand Down Expand Up @@ -172,7 +172,7 @@ export function exportLexicalTreeToMdast({
...props,
...(hasChildren ? { children: [] } : {})
}
appendToParent(mdastParent!, newNode as unknown as Mdast.Content)
appendToParent(mdastParent!, newNode as unknown as Mdast.RootContent)
if ($isElementNode(lexicalNode) && hasChildren) {
visitChildren(lexicalNode, newNode as Mdast.Parent)
}
Expand Down Expand Up @@ -251,7 +251,7 @@ export function exportLexicalTreeToMdast({
return typedRoot
}

function collapseNestedHtmlTags(node: Mdast.Parent | Mdast.Content) {
function collapseNestedHtmlTags(node: Mdast.Nodes) {
if ('children' in node && node.children) {
if (isMdastHTMLNode(node) && node.children.length === 1) {
const onlyChild = node.children[0]
Expand Down Expand Up @@ -282,10 +282,10 @@ function collapseNestedHtmlTags(node: Mdast.Parent | Mdast.Content) {
}
}

function convertUnderlineJsxToHtml(node: Mdast.Parent | Mdast.Content) {
function convertUnderlineJsxToHtml(node: Mdast.Parent | Mdast.RootContent) {
if (Object.hasOwn(node, 'children')) {
const nodeAsParent = node as Mdast.Parent
const newChildren = [] as Mdast.Content[]
const newChildren = [] as Mdast.RootContent[]
nodeAsParent.children.forEach((child) => {
if (child.type === 'mdxJsxTextElement' && child.name === 'u') {
newChildren.push(...[{ type: 'html', value: '<u>' } as const, ...child.children, { type: 'html', value: '</u>' } as const])
Expand All @@ -300,7 +300,7 @@ function convertUnderlineJsxToHtml(node: Mdast.Parent | Mdast.Content) {

const TRAILING_WHITESPACE_REGEXP = /\s+$/
const LEADING_WHITESPACE_REGEXP = /^\s+/
function fixWrappingWhitespace(node: Mdast.Parent | Mdast.Content, parentChain: Mdast.Parent[]) {
function fixWrappingWhitespace(node: Mdast.Parent | Mdast.RootContent, parentChain: Mdast.Parent[]) {
if (node.type === 'strong' || node.type === 'emphasis') {
const lastChild = node.children.at(-1)
if (lastChild?.type === 'text') {
Expand All @@ -309,7 +309,7 @@ function fixWrappingWhitespace(node: Mdast.Parent | Mdast.Content, parentChain:
lastChild.value = lastChild.value.replace(TRAILING_WHITESPACE_REGEXP, '')
const parent = parentChain.at(-1)
if (parent) {
parent.children.splice(parent.children.indexOf(node as unknown as Mdast.Content) + 1, 0, {
parent.children.splice(parent.children.indexOf(node as unknown as Mdast.RootContent) + 1, 0, {
type: 'text',
value: trailingWhitespace[0]
})
Expand All @@ -325,7 +325,7 @@ function fixWrappingWhitespace(node: Mdast.Parent | Mdast.Content, parentChain:

const parent = parentChain.at(-1)
if (parent) {
parent.children.splice(parent.children.indexOf(node as unknown as Mdast.Content), 0, {
parent.children.splice(parent.children.indexOf(node as unknown as Mdast.RootContent), 0, {
type: 'text',
value: leadingWhitespace[0]
})
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ export const viewMode$ = Cell<ViewMode>('rich-text', (r) => {
filter((mode) => mode.current === 'rich-text'),
withLatestFrom(activeEditor$)
),
([mode, editor]) => {
([, editor]) => {
editor?.dispatchCommand(NESTED_EDITOR_UPDATED_COMMAND, undefined)
}
)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/jsx/LexicalJsxVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const LexicalJsxVisitor: LexicalExportVisitor<LexicalJsxNode, MdxJsxFlowE
visitLexicalNode({ actions, mdastParent, lexicalNode }) {
function traverseNestedJsxNodes(node: Mdast.Content | Mdast.Parent) {
if ('children' in node && node.children instanceof Array) {
node.children.forEach((child: Mdast.Content | Mdast.Parent) => {
node.children.forEach((child: Mdast.Nodes) => {
if (isMdastJsxNode(child)) {
actions.registerReferredComponent(child.name!)
}
Expand Down

0 comments on commit 65df146

Please sign in to comment.