Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions fastify/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
import fastify from 'fastify';
import http from 'http';
import marked from 'marked';
import fastify from 'fastify'
import http from 'http'
import { marked } from 'marked'

declare function plugin<HttpServer = http.Server,
HttpRequest = http.IncomingMessage,
HttpResponse = http.ServerResponse
>(instance: fastify.FastifyInstance, opts: fastify.MarkdownOptions, callback: (err: Error) => void): void;
declare function plugin<
HttpServer = http.Server,
HttpRequest = http.IncomingMessage,
HttpResponse = http.ServerResponse
>(
instance: fastify.FastifyInstance,
opts: fastify.MarkdownOptions,
callback: (err: Error) => void
): void

declare module 'fastify' {
export interface MarkdownOptions {
src?: true | string;
data?: true | string;
markedOptions?: marked.MarkedOptions;
}
export interface MarkdownOptions {
src?: true | string
data?: true | string
markedOptions?: marked.MarkedExtension
}

interface FastifyReply<HttpResponse> {
code: (statusCode: number) => FastifyReply<HttpResponse>;
header: (name: string, value: any) => FastifyReply<HttpResponse>;
headers: (headers: { [key: string]: any }) => FastifyReply<HttpResponse>;
type: (contentType: string) => FastifyReply<HttpResponse>;
redirect: (statusCode: number, url: string) => FastifyReply<HttpResponse>;
serialize: (payload: any) => string;
serializer: (fn: Function) => FastifyReply<HttpResponse>;
send: (payload?: any) => FastifyReply<HttpResponse>;
sent: boolean;
res: HttpResponse;
context: FastifyContext;
interface FastifyReply<HttpResponse> {
code: (statusCode: number) => FastifyReply<HttpResponse>
header: (name: string, value: any) => FastifyReply<HttpResponse>
headers: (headers: { [key: string]: any }) => FastifyReply<HttpResponse>
type: (contentType: string) => FastifyReply<HttpResponse>
redirect: (statusCode: number, url: string) => FastifyReply<HttpResponse>
serialize: (payload: any) => string
serializer: (fn: Function) => FastifyReply<HttpResponse>
send: (payload?: any) => FastifyReply<HttpResponse>
sent: boolean
res: HttpResponse
context: FastifyContext

/**
* Plug-ins define features attached to reply
*/
markdown(md: string): string;
markdown(): typeof marked;
}
/**
* Plug-ins define features attached to reply
*/
markdown(md: string): string
markdown(): typeof marked
}
}

export = plugin;
export = plugin
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,27 @@ function fastifyMarkdown (fastify, opts, done) {

function asyncFileMarked (src, option) {
const read = util.promisify(fs.readFile)
return read(src, 'utf8').then(data => marked(data, option), err => err)
return read(src, 'utf8').then(
(data) => {
return marked.marked(data, option)
},
(err) => err
)
}

fastify.decorateReply('markdown', function (md) {
if (none(opts) && none(md)) return marked
if (none(opts) && have(md)) opts = {data: md}

if (none(opts) && have(md)) opts = { data: md }
if (opts.data) {
if (none(md) && isString(opts.data)) md = opts.data
return marked(md, markedOptions)
return marked.marked(md, markedOptions)
} else if (opts.src) {
if (none(md) && isString(opts.src)) md = opts.src
return asyncFileMarked(md, markedOptions)
} else if (opts.markedOptions) {
return marked.setOptions(markedOptions)
return marked.marked.setOptions(markedOptions)
} else {
return marked
return marked.marked
}
})

Expand Down
Loading