Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript support (request) #60

Open
nicobrinkkemper opened this issue Feb 2, 2018 · 3 comments
Open

typescript support (request) #60

nicobrinkkemper opened this issue Feb 2, 2018 · 3 comments

Comments

@nicobrinkkemper
Copy link

As the title suggest. The following is a temporary fix, but not ideal:

declare module "marksy" {
  const value: any
  export default value
}
declare module "marksy/components" {
  const value: any
  export default value
}
@vladanpaunovic
Copy link

vladanpaunovic commented Nov 4, 2019

Hi @nicobrinkkemper 👋
I did something for our own purposes. It doesn't cover all the tags, but most of them are covered. Feel free to expand it.

  1. You need to install @types/marked
  2. Create a file marksy.d.ts
    Add this content into that file:
interface MarksyCompiler {
  tree: ReactNode;
  toc: any;
}

interface HeadingProps {
  children: any;
  id: string;
}

interface NodeProps {
  children: any;
}

interface ImgProps {
  src: string;
  alt: string;
}

interface AnchorProps {
  children: any;
  href: string;
  title?: string;
  target?: "_blank" | "_self" | "_parent" | "_top";
}

interface MarksyClientConstructor {
  createElement: any;
  elements: {
    h1(props: HeadingProps): React.ReactNode;
    h2(props: HeadingProps): React.ReactNode;
    h3(props: HeadingProps): React.ReactNode;
    h4(props: HeadingProps): React.ReactNode;
    a(props: AnchorProps): React.ReactNode;
    img(props: ImgProps): React.ReactNode;
    p(props: NodeProps): React.ReactNode;
    strong(props: NodeProps): React.ReactNode;
    em(props: NodeProps): React.ReactNode;
    ol(props: NodeProps): React.ReactNode;
    ul(props: NodeProps): React.ReactNode;
    br(): React.ReactNode;
    hr(): React.ReactNode;
  };
  components?: Record<string, (props: Record<string, any>) => ReactNode>;
}

declare module "marksy" {
  function marksy(
    settings: MarksyClientConstructor
  ): (
    markdown: string,
    options?: import("@types/marked").MarkedOptions
  ) => MarksyCompiler;

  export default marksy;
}

@nicobrinkkemper
Copy link
Author

nicobrinkkemper commented May 20, 2021

I had to write it like this:

declare module "marksy" {
    import { ReactNode, createElement } from "react";

    interface MarksyCompiler {
        tree: ReactNode;
        toc: any;
    }

    interface HeadingProps {
        children: any;
        id: string;
    }

    interface NodeProps {
        children: any;
    }

    interface ImgProps {
        src: string;
        alt: string;
    }

    interface AnchorProps {
        children: any;
        href: string;
        title?: string;
        target?: "_blank" | "_self" | "_parent" | "_top";
    }

    interface CodeProps {
        children: any;
        language: string;
        code: string;
    }

    interface MarksyClientConstructor {
        createElement: typeof createElement;
        elements?: Partial<{
            h1(props: HeadingProps): React.ReactNode;
            h2(props: HeadingProps): React.ReactNode;
            h3(props: HeadingProps): React.ReactNode;
            h4(props: HeadingProps): React.ReactNode;
            a(props: AnchorProps): React.ReactNode;
            img(props: ImgProps): React.ReactNode;
            p(props: NodeProps): React.ReactNode;
            del(props: NodeProps): React.ReactNode;
            strong(props: NodeProps): React.ReactNode;
            blockquote(props: NodeProps): React.ReactNode;
            em(props: NodeProps): React.ReactNode;
            ol(props: NodeProps): React.ReactNode;
            ul(props: NodeProps): React.ReactNode;
            br(): React.ReactNode;
            hr(): React.ReactNode;
            code(props: CodeProps): React.ReactNode;
            codespan(props: NodeProps): React.ReactNode;
        }>;
        components?: Record<string, (props: Record<string, any>) => ReactNode>;
    }
    function marksy(
        settings: MarksyClientConstructor
    ): (markdown: string, options?: import("marked").MarkedOptions) => MarksyCompiler;

    export default marksy;
}

@jimmyandrade
Copy link
Member

Hello! This is awesome! Could you create a PR for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants