Skip to content

Commit

Permalink
Merge f963ebc into 1e24513
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon authored Oct 23, 2018
2 parents 1e24513 + f963ebc commit 4b4d912
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/event-emitter/oneshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface CustomPayload {
reset: undefined;
}

export interface CustomListener {
export interface CustomListener{
hoge: (payload: CustomPayload['hoge']) => void;
foo: (payload: CustomPayload['foo']) => void;
add: (payload: CustomPayload['add']) => void;
Expand All @@ -46,11 +46,11 @@ export class CustomEventEmitter extends EventEmitter {
}

public on<K extends keyof CustomListener>(event: K, listener : CustomListener[K]): this {
return super.on(event as string, listener);
return super.on(event, listener);
}

public addListener<K extends keyof CustomListener>(event: K, listener : CustomListener[K]): this {
return super.addListener(event as string, listener);
return super.addListener(event, listener);
}

public emit<K extends keyof CustomListener>(event: K, payload: CustomPayload[K]): boolean {
Expand Down
11 changes: 11 additions & 0 deletions src/react-markdown/heading1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as React from 'react'

export interface Heading1Props {
title: string
}

export class Heading1 extends React.Component<Heading1Props, {}> {
public render() {
return <h1>{this.props.title}</h1>
}
}
95 changes: 95 additions & 0 deletions src/react-markdown/markdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import * as React from 'react'
// @ts-ignore
import * as remark from 'remark'
// @ts-ignore
import * as remarkReact from 'remark-react'
// @ts-ignore
import * as remarkSlug from 'remark-slug'

import { Heading1, Heading1Props } from './heading1';

export interface ScopedComponents {
Title: () => React.Node
}


export interface MarkdownProps {
h1: { [key: string]: number }
scope: ScopedComponents
}

export interface MappedScope {
h1: React.ReactNode | undefined;
}

export interface HeadingProps {
id: string
children?: React.ReactNode[]
}

const defaultProps: MarkdownProps = {
h1: {
mb: 3,
mt: 4,
},
scope: {
Title: (props: Heading1Props) => <Heading1 {...props} />
}
}

const heading = (Comp: any) => (props: HeadingProps): React.ReactNode => {
return React.createElement(
Comp,
props,
React.createElement(
'a',
{
href: '#' + props.id,
style: {
color: 'inherit',
textDecoration: 'none',
},
},
props.children,
),
)
}

export class Markdown extends React.Component<MarkdownProps, {}> {
public render() {
const scope = this.props.scope;

const mappedScope = this.mapScope({ ...scope })
const remarkReactComponents = this.applyProps(mappedScope)

const opts = {
// pass Lab components to remark-react for rendering
remarkReactComponents,
}
// @ts-ignore
const element = remark()
.use(remarkSlug)
.use(remarkReact, opts)
.processSync(text).contents

return element
}

private mapScope = (scope: ScopedComponents): MappedScope => {
const h1 = scope.Title
return {
h1: h1 ? heading(h1) : undefined,
}
}

private applyProps = (scope: MappedScope) => {
const props = { ...defaultProps, ...options.markdownProps }
Object.keys(props).forEach(key => {
if (!scope[key]) {
return
}
scope[key].defaultProps = { ...scope[key].defaultProps, ...props[key] }
})
return scope
}
}

0 comments on commit 4b4d912

Please sign in to comment.