Skip to content

Commit

Permalink
fix: mdx typecheck
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Nov 10, 2024
1 parent 3109281 commit 8ba0c3f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 45 deletions.
3 changes: 2 additions & 1 deletion packages/mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"scripts": {
"dev": "npm run build -- --watch",
"build": "tsup src/mdx.ts",
"tsc": "tsc src/mdx.ts --noEmit --skipLibCheck",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
},
Expand Down Expand Up @@ -43,7 +44,7 @@
"@types/hash-sum": "^1.0",
"@types/hast": "^3.0.4",
"hast-util-raw": "^9.0.4",
"mdast-util-mdx-expression": "^2.0.0",
"mdast-util-mdx-expression": "^2.0.1",
"tsup": "8.2.4",
"typescript": "^5.6.3",
"unified": "^11.0.5",
Expand Down
10 changes: 5 additions & 5 deletions packages/mdx/src/rehype-raw-expressions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Identifier, CallExpression, VariableDeclarator, Expression, ExpressionStatement } from 'estree'
import type { Identifier, CallExpression, VariableDeclarator, Expression } from 'estree'
import type { MdxFlowExpression } from 'mdast-util-mdx-expression'
import type { MdxjsEsm } from 'mdast-util-mdxjs-esm'
import type { Plugin } from 'unified'
import type { Parent, Content, Element } from 'hast'
import { toHtml as hastToHtml, Options as ToHtmlOptions } from 'hast-util-to-html'
import { toHtml as hastToHtml, type Options as ToHtmlOptions } from 'hast-util-to-html'
import type { MarkdownOptions } from './types'

type Child = Content
Expand All @@ -26,7 +26,7 @@ export const rehypeRawExpressions: RawPlugin = options => (ast, vfile) => {
dynamicElements.add('excerpt')

const enter: Visitor = (node) => {
if (node.type === 'mdxFlowExpression' && node.data?.raw)
if (node.type === 'mdxFlowExpression' && (node.data as any)?.raw)
// @ts-ignore
node.type = 'raw'

Expand Down Expand Up @@ -103,11 +103,11 @@ function stringifyNodes (hoisted: Hoisted, nodes: Child[]) {
}

function isDynamic (node: Node) {
return node.data?._createVNode
return (node.data as any)?._createVNode
}

function setDynamic (node: Node) {
(node.data ||= {})._createVNode = true
((node.data ||= {}) as any)._createVNode = true
}

function hoistRawNodes (hoisted: Hoisted, nodes: Child[]): MdxFlowExpression {
Expand Down
12 changes: 5 additions & 7 deletions packages/mdx/src/remark-internal-hrefs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type { MDXJsxFlowElement, MDXJsxTextElement } from 'mdast-util-mdx-jsx'
import type { MDXJSEsm } from 'mdast-util-mdxjs-esm'
import type { Root, Link } from 'mdast'
import type { MdxJsxFlowElement, MdxJsxTextElement } from 'mdast-util-mdx-jsx'
import type { Root } from 'mdast'
import type { Plugin, Transformer } from 'unified'
import type { Parent, Node } from 'unist'

import { visit, SKIP } from 'unist-util-visit'

import { isExternal, isJsxElement, isString, toExplicitHtmlPath } from './utils'
import { isJsxElement, isString, toExplicitHtmlPath } from './utils'

export interface HrefOptions {
prettyUrls?: boolean
Expand All @@ -16,7 +14,7 @@ type HrefPlugin = Plugin<[HrefOptions?], Root, Root>
type HrefProcessor = Transformer<Root, Root>

/**
* A Remark plugin for converting Markdown images to MDX images using imports
* A Remark plugin for converting Markdown images to Mdx images using imports
* for the image source.
*/
export const remarkInternalHrefs: HrefPlugin = (options) => {
Expand All @@ -37,7 +35,7 @@ const remarkProcessor: HrefProcessor = (ast, vfile) => {
}
})

function replaceHrefAttribute (node: MDXJsxTextElement | MDXJsxFlowElement) {
function replaceHrefAttribute (node: MdxJsxTextElement | MdxJsxFlowElement) {
for (const attr of node.attributes) {
if (attr.type === 'mdxJsxAttribute' && attr.name === 'href') {
if (isString(attr.value) && attr.value) attr.value = toExplicitHtmlPath(attr.value)
Expand Down
17 changes: 8 additions & 9 deletions packages/mdx/src/remark-mdx-images.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { MDXJsxFlowElement, MDXJsxTextElement, MDXJsxAttribute, MDXJsxAttributeValueExpression } from 'mdast-util-mdx-jsx'
import type { MDXJSEsm } from 'mdast-util-mdxjs-esm'
import type { MdxJsxFlowElement, MdxJsxTextElement, MdxJsxAttributeValueExpression } from 'mdast-util-mdx-jsx'
import type { MdxjsEsm } from 'mdast-util-mdxjs-esm'
import type { Root, Image } from 'mdast'
import type { Plugin } from 'unified'
import type { Parent, Node } from 'unist'
import type { VFile } from 'vfile'
import type { Parent } from 'unist'
import { visit, SKIP } from 'unist-util-visit'
import type { MarkdownOptions } from './types'

Expand All @@ -12,11 +11,11 @@ import { isAbsolute, isJsxElement, isString } from './utils'
type ImagePlugin = Plugin<[MarkdownOptions?], Root, Root>

/**
* A Remark plugin for converting Markdown images to MDX images using imports
* A Remark plugin for converting Markdown images to Mdx images using imports
* for the image source.
*/
export const remarkMdxImages: ImagePlugin = options => (ast, vfile) => {
const imports: MDXJSEsm[] = []
const imports: MdxjsEsm[] = []
const imported = new Map<string, string>()

visit(ast, (node, index, parent) => {
Expand All @@ -30,7 +29,7 @@ export const remarkMdxImages: ImagePlugin = options => (ast, vfile) => {
if (imports.length > 0)
ast.children.unshift(...imports)

function replaceSrcAttribute (node: MDXJsxTextElement | MDXJsxFlowElement) {
function replaceSrcAttribute (node: MdxJsxTextElement | MdxJsxFlowElement) {
for (const attr of node.attributes) {
if (attr.type === 'mdxJsxAttribute' && attr.name === 'src' && isString(attr.value)) {
const srcExpression = imageSrcToMdxExpression(attr.value)
Expand All @@ -48,7 +47,7 @@ export const remarkMdxImages: ImagePlugin = options => (ast, vfile) => {
const attrs = { alt: node.alt || null, title: node.title, src }
if (!node.title) delete attrs.title

const mdxImage: MDXJsxTextElement = {
const mdxImage: MdxJsxTextElement = {
type: 'mdxJsxTextElement',
name: 'img',
children: [],
Expand All @@ -62,7 +61,7 @@ export const remarkMdxImages: ImagePlugin = options => (ast, vfile) => {
return SKIP
}

function imageSrcToMdxExpression (url: string): undefined | MDXJsxAttributeValueExpression {
function imageSrcToMdxExpression (url: string): undefined | MdxJsxAttributeValueExpression {
const name = imageSrcToIdentifier(url)
if (!name) return
return {
Expand Down
15 changes: 3 additions & 12 deletions packages/mdx/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
/// <reference types="mdast-util-mdx-expression" />
/// <reference types="mdast-util-mdxjs-esm" />

import type { CompileOptions } from '@mdx-js/mdx'
import type { Pluggable } from 'unified'
import type { VFile } from 'vfile'

export type PluginLike = null | undefined | false | Pluggable
export type PluginOption = PluginLike | Promise<PluginLike> | string | [string, any]

import type { MdxFlowExpression } from 'mdast-util-mdx-expression'

declare module 'hast' {
interface RootContentMap {
mdxFlowExpression: MdxFlowExpression
}

interface ElementContentMap {
mdxFlowExpression: MdxFlowExpression
}
}

export interface MarkdownOptions extends Omit<CompileOptions, 'remarkPlugins' | 'rehypePlugins' | 'recmaPlugins'> {
/**
* Recma plugins that should be used to process files.
Expand Down
4 changes: 2 additions & 2 deletions packages/mdx/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extname } from 'path'
import type { Node } from 'unist'
import type { MDXJsxTextElement, MDXJsxFlowElement } from 'mdast-util-mdx-jsx'
import type { MdxJsxTextElement, MdxJsxFlowElement } from 'mdast-util-mdx-jsx'

const urlPattern = /^(https?:)?\//
const externalUrlPattern = /^(https?:)?\/\//
Expand All @@ -13,7 +13,7 @@ export function isExternal (url: string) {
return externalUrlPattern.test(url)
}

export function isJsxElement (node: Node): node is MDXJsxTextElement | MDXJsxFlowElement {
export function isJsxElement (node: Node): node is MdxJsxTextElement | MdxJsxFlowElement {
return node.type === 'mdxJsxTextElement' || node.type === 'mdxJsxFlowElement'
}

Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8ba0c3f

Please sign in to comment.