Skip to content

Commit

Permalink
Update typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tims777 committed Apr 18, 2023
1 parent 52cbaea commit 490edea
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 45 deletions.
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { visit as unistVisit } from "https://esm.sh/[email protected]";
export { configureAll } from "./lib/directives.ts";
export { parseMarkdown } from "./lib/markdown.ts";
export { preactify } from "./lib/preactify.ts";
export { createElement, Fragment } from "preact";
16 changes: 8 additions & 8 deletions lib/directives.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { unistVisit } from "../deps.ts";
import {
type ConfiguredDirective,
type Directive,
type DirectiveHandler,
type DirectiveOptions,
type Mdast,
type SkippedDirective,
import type {
ConfiguredDirective,
Directive,
DirectiveHandler,
DirectiveOptions,
Mdast,
SkippedDirective,
} from "../types.d.ts";

enum DirectiveType {
Expand Down Expand Up @@ -40,7 +40,7 @@ function defaultConfigurator(directive: Directive) {
export async function configure<TContext>(
directive: Directive,
handler?: DirectiveHandler,
context?: TContext
context?: TContext,
) {
const result = handler
? await (handler.configure ?? defaultConfigurator)(directive, context)
Expand Down
2 changes: 1 addition & 1 deletion lib/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
gfm,
gfmFromMarkdown,
} from "../deps.ts";
import { type Mdast } from "../types.d.ts";
import type { Mdast } from "../types.d.ts";

export function parseMarkdown(markdown: string): Mdast {
return fromMarkdown(markdown, "utf-8", {
Expand Down
20 changes: 10 additions & 10 deletions lib/preactify.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { createElement, Fragment } from "preact";
import { toHast } from "../deps.ts";
import {
type ConfiguredDirective,
type DirectiveOptions,
type HastNode,
type Mdast,
type SkippedDirective,
type VNode,
import { createElement, Fragment, toHast } from "../deps.ts";
import type {
ConfiguredDirective,
DirectiveOptions,
HastNode,
Mdast,
SkippedDirective,
VNode,
} from "../types.d.ts";

export function preactify(
Expand All @@ -27,7 +26,8 @@ function preactifyHastNode(
): VNode | null {
if (node.type == "configuredDirective") {
return createElement(
directives[node.name].component,
// deno-lint-ignore no-explicit-any
directives[node.name].component as any,
node.properties,
node.properties.children ?? preactifyChildren(node, directives),
);
Expand Down
10 changes: 5 additions & 5 deletions test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import MarkdownPreactifier from "./mod.ts";
import { assertEquals, renderToString } from "./test-deps.ts";
import {
type ComponentChildren,
type ComponentConfigurator,
type DirectiveOptions,
type VNode,
import type {
ComponentChildren,
ComponentConfigurator,
DirectiveOptions,
VNode,
} from "./types.d.ts";

interface CustomDirectiveProps {
Expand Down
45 changes: 24 additions & 21 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
/// <reference no-default-lib="true" />
/// <reference lib="deno.ns" />

export { type Root as Mdast } from "https://esm.sh/v114/@types/[email protected]/index.d.ts";
export type { Root as Mdast } from "https://esm.sh/v114/@types/[email protected]/index.d.ts";

import {
type Comment,
type DocType,
type Element,
type ElementContent,
type Root,
type Text,
import type {
Comment,
DocType,
Element,
ElementContent,
Root,
Text,
} from "https://esm.sh/v113/@types/[email protected]/index.d.ts";

export type HastNode = Root | Element | DocType | Comment | Text;

export {
type ComponentChildren,
type ComponentType,
type VNode,
export type {
ComponentChildren,
ComponentType,
VNode,
} from "https://esm.sh/v114/[email protected]/src/index.d.ts";

import {
type ContainerDirective,
type Directive,
type LeafDirective,
type TextDirective,
import type {
ContainerDirective,
Directive,
LeafDirective,
TextDirective,
} from "https://esm.sh/v114/[email protected]/index.d.ts";

export { type Directive };
export type { Directive };

declare module "https://esm.sh/v114/@types/[email protected]/index.d.ts" {
interface StaticPhrasingContentMap {
Expand All @@ -39,18 +39,21 @@ declare module "https://esm.sh/v114/@types/[email protected]/index.d.ts" {
}
}

import { type ComponentType } from "https://esm.sh/v114/[email protected]/src/index.d.ts";
import type {
ComponentType,
JSX,
} from "https://esm.sh/v114/[email protected]/src/index.d.ts";

// deno-lint-ignore ban-types no-explicit-any
export interface DirectiveHandler<P extends {} = any, C = any> {
component: ComponentType<P>;
component: ComponentType<P> | keyof JSX.IntrinsicElements;
configure?: ComponentConfigurator<P, C>;
}

export type MaybePromise<T> = Promise<T> | T;
export type ComponentConfigurator<TResult, TContext = void> = (
directive: Directive,
context?: TContext
context?: TContext,
) => MaybePromise<TResult | false>;
export type DirectiveOptions = Record<string, DirectiveHandler>;

Expand Down

0 comments on commit 490edea

Please sign in to comment.