fastify-markdown 0.4.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2017-2018 freezestudio
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2018 freezestudio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/Readme.md CHANGED
@@ -1,121 +1,122 @@
1
- # fastify-markdown
2
-
3
- [![Greenkeeper badge](https://badges.greenkeeper.io/fastify/fastify-plugin.svg)](https://greenkeeper.io/)
4
-
5
- [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
6
- [![Build Status](https://travis-ci.org/freezestudio/fastify-markdown.svg?branch=master)](https://travis-ci.org/freezestudio/fastify-markdown)
7
-
8
- `fastify-markdown` is a plugin for [Fastify](https://github.com/fastify/fastify) parse markdown code or file.
9
-
10
- ## Install
11
-
12
- npm install --save fastify-markdown
13
-
14
- ## Usage
15
-
16
- ```js
17
- const path = require('path')
18
- const fastify = require('fastify')({ logger: { level: 'trace' } })
19
-
20
- /**
21
- * using opts.src read markdown from file
22
- */
23
- fastify
24
- .register(require('fastify-markdown'), { src: true })
25
- .get('/', (req, reply) => {
26
- return reply.markdown(path.join(__dirname, '..', 'Readme.md'))
27
- })
28
- .listen(3000, err => {
29
- if (err) throw err
30
- else console.log('server running on http://localhost:3000 ...')
31
- })
32
-
33
- /* -- or --*/
34
-
35
- /**
36
- * async using opts.src from file
37
- */
38
- fastify
39
- .register(require('../'), {
40
- src: true, markedOptions: { gfm: false }
41
- })
42
- .get('/', async (req, reply) => {
43
- const md = await reply.markdown(path.join(__dirname, '..', 'Readme.md'))
44
- return md
45
- })
46
- .listen(3000, err => {
47
- if (err) throw err
48
- else console.log('server running on http://localhost:3000 ...')
49
- })
50
-
51
- /**
52
- * using opts.data parse markdown direct literal
53
- */
54
- fastify
55
- .register(require('fastify-markdown'), { data: true })
56
- .get('/', (req, reply) => {
57
- const md = reply.markdown(`**BOLD**`)
58
- reply.send(md)
59
- })
60
- .listen(3000, err => {
61
- if (err) throw err
62
- else console.log('server running on http://localhost:3000 ...')
63
- })
64
-
65
- /**
66
- * using internal marked's options
67
- */
68
- const testOptions = {
69
- gfm: false
70
- }
71
-
72
- fastify
73
- .register(require('fastify-markdown'), { markedOptions: testOptions })
74
- .get('/', (req, reply) => {
75
- const md = reply.markdown('**BOLD**')
76
- reply.send(md)
77
- })
78
- .listen(3000, err => {
79
- if (err) throw err
80
- else console.log('server running on http://localhost:3000 ...')
81
- })
82
-
83
- /**
84
- * using marked as object see:https://marked.js.org/
85
- */
86
- fastify
87
- .register(require('fastify-markdown'), /*non options or other*/)
88
- .get('/', (req, reply) => {
89
- const md = reply.markdown().parse('**bold title**')
90
- reply.send(md)
91
- })
92
- .listen(3000, err => {
93
- if (err) throw err
94
- else console.log('server running on http://localhost:3000 ...')
95
- })
96
-
97
- ```
98
-
99
- ## Options (Optional)
100
-
101
- * **src** (boolean | string)
102
- * `true`: Means to resolve the markdown file.
103
- * `string`(deprecated): the .md file path.If the `data` option is set, this option will be ignored.
104
- * **data** (boolean | string)
105
- * `true`: Means to resolve the markdown data.
106
- * `string`(deprecated): or a string that conforms to the markdown syntax.
107
- * **markedOptions** (object)
108
- * marked options used
109
-
110
- See [marked](https://github.com/markedjs/marked) and [fastify](https://github.com/fastify/fastify) for more options
111
-
112
- All options are optional. in this case as if using `opts.data`.
113
-
114
- > **Note**
115
- > Can be set to any option other than those listed above, in which case the internal markdown parser will be returned.
116
-
117
- Typescript usage see [wiki](https://github.com/freezestudio/fastify-markdown/wiki)
118
-
119
- ## License
120
-
121
- Licensed under [MIT](./LICENSE).
1
+ # fastify-markdown
2
+
3
+ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
4
+ [![CI](https://github.com/freezestudio/fastify-markdown/actions/workflows/ci.yml/badge.svg)](https://github.com/freezestudio/fastify-markdown/actions/workflows/ci.yml)
5
+ [![npm](https://img.shields.io/npm/v/fastify-markdown.svg)](https://www.npmjs.com/package/fastify-markdown)
6
+
7
+ `fastify-markdown` is a plugin for [Fastify](https://github.com/fastify/fastify) (v5+) to parse markdown code or files.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install fastify-markdown
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```js
18
+ const path = require('path')
19
+ const fastify = require('fastify')({ logger: { level: 'trace' } })
20
+
21
+ /**
22
+ * using opts.src read markdown from file
23
+ */
24
+ fastify
25
+ .register(require('fastify-markdown'), { src: true })
26
+ .get('/', (req, reply) => {
27
+ return reply.markdown(path.join(__dirname, 'Readme.md'))
28
+ })
29
+ .listen({ port: 3000 }, err => {
30
+ if (err) throw err
31
+ else console.log('server running on http://localhost:3000 ...')
32
+ })
33
+
34
+ /* -- or --*/
35
+
36
+ /**
37
+ * async using opts.src from file
38
+ */
39
+ fastify
40
+ .register(require('fastify-markdown'), {
41
+ src: true, markedOptions: { gfm: false }
42
+ })
43
+ .get('/', async (req, reply) => {
44
+ const md = await reply.markdown(path.join(__dirname, '..', 'Readme.md'))
45
+ return md
46
+ })
47
+ .listen({ port: 3000 }, err => {
48
+ if (err) throw err
49
+ else console.log('server running on http://localhost:3000 ...')
50
+ })
51
+
52
+ /**
53
+ * using opts.data parse markdown direct literal
54
+ */
55
+ fastify
56
+ .register(require('fastify-markdown'), { data: true })
57
+ .get('/', (req, reply) => {
58
+ const md = reply.markdown(`**BOLD**`)
59
+ reply.send(md)
60
+ })
61
+ .listen({ port: 3000 }, err => {
62
+ if (err) throw err
63
+ else console.log('server running on http://localhost:3000 ...')
64
+ })
65
+
66
+ /**
67
+ * using internal marked's options
68
+ */
69
+ const testOptions = {
70
+ gfm: false
71
+ }
72
+
73
+ fastify
74
+ .register(require('fastify-markdown'), { markedOptions: testOptions })
75
+ .get('/', (req, reply) => {
76
+ const md = reply.markdown('**BOLD**')
77
+ reply.send(md)
78
+ })
79
+ .listen({ port: 3000 }, err => {
80
+ if (err) throw err
81
+ else console.log('server running on http://localhost:3000 ...')
82
+ })
83
+
84
+ /**
85
+ * using marked as object see:https://marked.js.org/
86
+ */
87
+ fastify
88
+ .register(require('fastify-markdown') /*non options or other*/)
89
+ .get('/', (req, reply) => {
90
+ const md = reply.markdown().parse('**bold title**')
91
+ reply.send(md)
92
+ })
93
+ .listen({ port: 3000 }, err => {
94
+ if (err) throw err
95
+ else console.log('server running on http://localhost:3000 ...')
96
+ })
97
+
98
+ ```
99
+
100
+ ## Options (Optional)
101
+
102
+ * **src** (boolean | string)
103
+ * `true`: Means to resolve the markdown file.
104
+ * `string`(deprecated): the .md file path.If the `data` option is set, this option will be ignored.
105
+ * **data** (boolean | string)
106
+ * `true`: Means to resolve the markdown data.
107
+ * `string`(deprecated): or a string that conforms to the markdown syntax.
108
+ * **markedOptions** (object)
109
+ * marked options used
110
+
111
+ See [marked](https://github.com/markedjs/marked) and [fastify](https://github.com/fastify/fastify) for more options
112
+
113
+ All options are optional. in this case as if using `opts.data`.
114
+
115
+ > **Note**
116
+ > Can be set to any option other than those listed above, in which case the internal markdown parser will be returned.
117
+
118
+ TypeScript usage see [wiki](https://github.com/freezestudio/fastify-markdown/wiki)
119
+
120
+ ## License
121
+
122
+ Licensed under [MIT](./LICENSE).
@@ -1,38 +1,22 @@
1
- import fastify from 'fastify';
2
- import http from 'http';
3
- import marked from 'marked';
4
-
5
- declare function plugin<HttpServer = http.Server,
6
- HttpRequest = http.IncomingMessage,
7
- HttpResponse = http.ServerResponse
8
- >(instance: fastify.FastifyInstance, opts: fastify.MarkdownOptions, callback: (err: Error) => void): void;
9
-
10
- declare module 'fastify' {
11
- export interface MarkdownOptions {
12
- src?: true | string;
13
- data?: true | string;
14
- markedOptions?: marked.MarkedOptions;
15
- }
16
-
17
- interface FastifyReply<HttpResponse> {
18
- code: (statusCode: number) => FastifyReply<HttpResponse>;
19
- header: (name: string, value: any) => FastifyReply<HttpResponse>;
20
- headers: (headers: { [key: string]: any }) => FastifyReply<HttpResponse>;
21
- type: (contentType: string) => FastifyReply<HttpResponse>;
22
- redirect: (statusCode: number, url: string) => FastifyReply<HttpResponse>;
23
- serialize: (payload: any) => string;
24
- serializer: (fn: Function) => FastifyReply<HttpResponse>;
25
- send: (payload?: any) => FastifyReply<HttpResponse>;
26
- sent: boolean;
27
- res: HttpResponse;
28
- context: FastifyContext;
29
-
30
- /**
31
- * Plug-ins define features attached to reply
32
- */
33
- markdown(md: string): string;
34
- markdown(): typeof marked;
35
- }
36
- }
37
-
38
- export = plugin;
1
+ import { FastifyPluginAsync } from 'fastify'
2
+ import { MarkedOptions, marked } from 'marked'
3
+
4
+ declare module 'fastify' {
5
+ interface FastifyReply {
6
+ /**
7
+ * Plug-ins define features attached to reply
8
+ */
9
+ markdown(md: string): string | Promise<string>
10
+ markdown(): typeof marked
11
+ }
12
+ }
13
+
14
+ export interface FastifyMarkdownOptions {
15
+ src?: true | string
16
+ data?: true | string
17
+ markedOptions?: MarkedOptions
18
+ }
19
+
20
+ declare const fastifyMarkdown: FastifyPluginAsync<FastifyMarkdownOptions>
21
+
22
+ export = fastifyMarkdown
package/index.js CHANGED
@@ -1,59 +1,49 @@
1
- 'use strict'
2
-
3
- const fs = require('fs')
4
- const util = require('util')
5
- const marked = require('marked')
6
-
7
- const fp = require('fastify-plugin')
8
-
9
- function fastifyMarkdown (fastify, opts, done) {
10
- if (typeof opts === 'function') {
11
- done = opts
12
- opts = undefined
13
- }
14
-
15
- const markedOptions = opts.markedOptions || {}
16
-
17
- function none (obj) {
18
- if (obj === undefined) return true
19
- if (obj && Object.getOwnPropertyNames(obj).length === 0) return true
20
- return false
21
- }
22
-
23
- function have (obj) {
24
- return !!obj
25
- }
26
-
27
- function isString (str) {
28
- return typeof str === 'string'
29
- }
30
-
31
- function asyncFileMarked (src, option) {
32
- const read = util.promisify(fs.readFile)
33
- return read(src, 'utf8').then(data => marked(data, option), err => err)
34
- }
35
-
36
- fastify.decorateReply('markdown', function (md) {
37
- if (none(opts) && none(md)) return marked
38
- if (none(opts) && have(md)) opts = {data: md}
39
-
40
- if (opts.data) {
41
- if (none(md) && isString(opts.data)) md = opts.data
42
- return marked(md, markedOptions)
43
- } else if (opts.src) {
44
- if (none(md) && isString(opts.src)) md = opts.src
45
- return asyncFileMarked(md, markedOptions)
46
- } else if (opts.markedOptions) {
47
- return marked.setOptions(markedOptions)
48
- } else {
49
- return marked
50
- }
51
- })
52
-
53
- done()
54
- }
55
-
56
- module.exports = fp(fastifyMarkdown, {
57
- fastify: '>=1.4.0',
58
- name: 'fastify-markdown'
59
- })
1
+ 'use strict'
2
+
3
+ const fs = require('fs')
4
+ const util = require('util')
5
+ const { marked } = require('marked')
6
+
7
+ const fp = require('fastify-plugin')
8
+
9
+ function none (obj) {
10
+ if (obj == null) return true
11
+ if (typeof obj === 'object' && Object.getOwnPropertyNames(obj).length === 0) return true
12
+ return false
13
+ }
14
+
15
+ function isString (str) {
16
+ return typeof str === 'string'
17
+ }
18
+
19
+ function asyncFileMarked (src, option) {
20
+ const read = util.promisify(fs.readFile)
21
+ return read(src, 'utf8').then(data => marked.parse(data, option))
22
+ }
23
+
24
+ async function fastifyMarkdown (fastify, opts) {
25
+ const markedOptions = opts.markedOptions || {}
26
+
27
+ fastify.decorateReply('markdown', function (md) {
28
+ if (none(opts)) {
29
+ if (none(md)) return marked
30
+ return marked.parse(md, markedOptions)
31
+ }
32
+
33
+ if (opts.data) {
34
+ if (none(md) && isString(opts.data)) md = opts.data
35
+ return marked.parse(md, markedOptions)
36
+ } else if (opts.src) {
37
+ if (none(md) && isString(opts.src)) md = opts.src
38
+ return asyncFileMarked(md, markedOptions)
39
+ } else if (opts.markedOptions) {
40
+ return marked.setOptions(markedOptions)
41
+ }
42
+ return marked
43
+ })
44
+ }
45
+
46
+ module.exports = fp(fastifyMarkdown, {
47
+ fastify: '5.x',
48
+ name: 'fastify-markdown'
49
+ })
package/package.json CHANGED
@@ -1,44 +1,47 @@
1
- {
2
- "name": "fastify-markdown",
3
- "version": "0.4.0",
4
- "description": "Plugin for markdown parser",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "npm run lint && npm run unit",
8
- "lint": "standard | snazzy",
9
- "unit": "tap test",
10
- "example": "node example/server.js",
11
- "coverage": "tap --cov --coverage-report=html test",
12
- "coveralls": "tap *.test.js --cov"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://github.com/freezestudio/fastify-markdown.git"
17
- },
18
- "keywords": [
19
- "fastify",
20
- "markdown"
21
- ],
22
- "author": "freezestudio",
23
- "license": "MIT",
24
- "bugs": {
25
- "url": "https://github.com/freezestudio/fastify-markdown/issues"
26
- },
27
- "homepage": "https://github.com/freezestudio/fastify-markdown#readme",
28
- "dependencies": {
29
- "fastify-plugin": "^1.0.1",
30
- "marked": "^0.3.19"
31
- },
32
- "devDependencies": {
33
- "coveralls": "^3.0.1",
34
- "fastify": "^1.4.0",
35
- "fastify-compress": "^0.6.0",
36
- "pre-commit": "^1.2.2",
37
- "proxyquire": "^2.0.1",
38
- "request": "^2.87.0",
39
- "snazzy": "^7.1.1",
40
- "standard": "^11.0.1",
41
- "tap": "^12.0.1"
42
- },
43
- "types": "./fastify/index.d.ts"
44
- }
1
+ {
2
+ "name": "fastify-markdown",
3
+ "version": "0.5.1",
4
+ "description": "Plugin for markdown parser",
5
+ "main": "index.js",
6
+ "types": "./fastify/index.d.ts",
7
+ "files": [
8
+ "index.js",
9
+ "fastify/",
10
+ "LICENSE",
11
+ "Readme.md"
12
+ ],
13
+ "scripts": {
14
+ "test": "npm run lint && npm run unit",
15
+ "lint": "standard | snazzy",
16
+ "unit": "tap test",
17
+ "example": "node example/server.js",
18
+ "coverage": "tap --coverage-report=html test"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/freezestudio/fastify-markdown.git"
23
+ },
24
+ "keywords": [
25
+ "fastify",
26
+ "markdown"
27
+ ],
28
+ "author": "freezestudio",
29
+ "license": "MIT",
30
+ "bugs": {
31
+ "url": "https://github.com/freezestudio/fastify-markdown/issues"
32
+ },
33
+ "homepage": "https://github.com/freezestudio/fastify-markdown#readme",
34
+ "engines": {
35
+ "node": ">=20"
36
+ },
37
+ "dependencies": {
38
+ "fastify-plugin": "^6.0.0",
39
+ "marked": "^18.0.9"
40
+ },
41
+ "devDependencies": {
42
+ "fastify": "^5.12.0",
43
+ "snazzy": "^9.0.0",
44
+ "standard": "^17.1.2",
45
+ "tap": "^21.7.5"
46
+ }
47
+ }
package/.travis.yml DELETED
@@ -1,15 +0,0 @@
1
- language: node_js
2
-
3
- node_js:
4
- - "9"
5
- - "8"
6
- - "6"
7
- - "10"
8
-
9
- script:
10
- - npm run coveralls
11
-
12
- notifications:
13
- email:
14
- on_success: never
15
- on_failure: always
package/example/server.js DELETED
@@ -1,80 +0,0 @@
1
- const path = require('path')
2
- const fastify = require('fastify')({
3
- logger: {
4
- level: 'trace'
5
- }
6
- })
7
-
8
- /**
9
- * using opts.src = 'file'
10
- */
11
- // fastify
12
- // .register(require('../'), {
13
- // src: true, markedOptions: {gfm: false}
14
- // })
15
- // .get('/', (req, reply) => {
16
- // return reply.markdown(path.join(__dirname, '..', 'Readme.md'))
17
- // })
18
- // .listen(3000, err => {
19
- // if (err) throw err
20
- // else console.log('server running on http://localhost:3000 ...')
21
- // })
22
-
23
- /**
24
- * using opts.src = true
25
- */
26
- fastify
27
- .register(require('../'), {
28
- src: true, markedOptions: {gfm: false}
29
- })
30
- .get('/', (req, reply) => {
31
- return reply.markdown(path.join(__dirname, '..', 'Readme.md'))
32
- })
33
- .listen(3000, err => {
34
- if (err) throw err
35
- else console.log('server running on http://localhost:3000 ...')
36
- })
37
-
38
- /**
39
- * using opts.data = true
40
- */
41
- // fastify
42
- // .register(require('../'), {
43
- // data: true, markedOptions: {gfm: false}
44
- // })
45
- // .get('/', (req, reply) => {
46
- // const md = reply.markdown('**BOLD**')
47
- // reply.send(md)
48
- // })
49
- // .listen(3000, err => {
50
- // if (err) throw err
51
- // else console.log('server running on http://localhost:3000 ...')
52
- // })
53
-
54
- /**
55
- * default marked data
56
- */
57
- // fastify
58
- // .register(require('../'))
59
- // .get('/', (req, reply) => {
60
- // const md = reply.markdown('**BOLD**')
61
- // reply.send(md)
62
- // })
63
- // .listen(3000, err => {
64
- // if (err) throw err
65
- // else console.log('server running on http://localhost:3000 ...')
66
- // })
67
-
68
- /**
69
- * using internal marked and options
70
- */
71
- // fastify
72
- // .register(require('../'))
73
- // .get('/', (req, reply) => {
74
- // const md = reply.markdown().parse('**BOLD**', {breaks: true})
75
- // reply.send(md)
76
- // })
77
- // .listen(3000, err => {
78
- // if (err) throw err
79
- // else console.log('server running on http://localhost:3000 ...')
80
- // })
@@ -1,191 +0,0 @@
1
- 'use strict'
2
-
3
- const path = require('path')
4
- const test = require('tap').test
5
- const Fastify = require('fastify')
6
- const plugin = require('../')
7
-
8
- test('Should markdowon with "opts.data" is', t => {
9
- t.plan(2)
10
- const fastify = Fastify()
11
- fastify.register(plugin, {
12
- data: `**BOLD**`
13
- })
14
- fastify.get('/', (req, reply) => {
15
- const md = reply.markdown()
16
- reply.send(md)
17
- })
18
- fastify.inject({
19
- url: '/',
20
- method: 'GET'
21
- }, (err, res) => {
22
- t.error(err)
23
- t.strictEqual(res.payload.trim(), `<p><strong>BOLD</strong></p>`)
24
- })
25
- })
26
-
27
- test('Should markdowon with "opts.data" and "opts.markedOptions" is', t => {
28
- t.plan(2)
29
- const fastify = Fastify()
30
- fastify.register(plugin, {
31
- data: true, markedOptions: {gfm: false}
32
- })
33
- fastify.get('/', (req, reply) => {
34
- const md = reply.markdown(`**BOLD**`)
35
- reply.send(md)
36
- })
37
- fastify.inject({
38
- url: '/',
39
- method: 'GET'
40
- }, (err, res) => {
41
- t.error(err)
42
- t.strictEqual(res.payload, `<p><strong>BOLD</strong></p>\n`)
43
- })
44
- })
45
-
46
- test('Should markdown with "opts.src" is', t => {
47
- t.plan(2)
48
- const fastify = Fastify()
49
- fastify.register(plugin, {
50
- src: true
51
- })
52
- fastify.get('/', (req, reply) => {
53
- return reply.markdown(path.join(__dirname, '..', 'Readme.md'))
54
- })
55
- fastify.inject({
56
- url: '/',
57
- method: 'GET'
58
- }, (err, res) => {
59
- t.error(err)
60
- t.ok(res.payload)
61
- })
62
- })
63
-
64
- test('Should markdown with "opts.src" is incorrect', t => {
65
- t.plan(2)
66
- const fastify = Fastify()
67
- fastify.register(plugin, {
68
- src: 'empty'
69
- })
70
- fastify.get('/', (req, reply) => {
71
- reply.markdown().then(md => {
72
- reply.send(md)
73
- }).catch(err => {
74
- reply.send(err)
75
- })
76
- })
77
- fastify.inject({
78
- url: '/',
79
- method: 'GET'
80
- }, (err, res) => {
81
- t.error(err)
82
- t.strictEqual(JSON.parse(res.payload).statusCode, 500)
83
- })
84
- })
85
-
86
- test('Should markdown with "opts.markedOptions" is', t => {
87
- t.plan(2)
88
- const testOptions = {
89
- gfm: false
90
- }
91
- const fastify = Fastify()
92
- fastify.register(plugin, {
93
- markedOptions: testOptions
94
- })
95
- fastify.get('/', (req, reply) => {
96
- const gfm = reply.markdown().defaults.gfm
97
- reply.send(gfm)
98
- })
99
- fastify.inject({
100
- url: '/',
101
- method: 'GET'
102
- }, (err, res) => {
103
- t.error(err)
104
- t.equal(res.payload, 'false')
105
- })
106
- })
107
-
108
- test('Should markdown without "opts" is', t => {
109
- t.plan(2)
110
- const fastify = Fastify()
111
- fastify.register(plugin /*, opts */)
112
- fastify.get('/', (req, reply) => {
113
- const md = reply.markdown().parse(`**BOLD**`)
114
- reply.send(md)
115
- })
116
- fastify.inject({
117
- url: '/',
118
- method: 'GET'
119
- }, (err, res) => {
120
- t.error(err)
121
- t.strictEqual(res.payload.trim(), `<p><strong>BOLD</strong></p>`)
122
- })
123
- })
124
-
125
- test('Should markdown without "opts" and "markdown" method with "md" param is', t => {
126
- t.plan(2)
127
- const fastify = Fastify()
128
- fastify.register(plugin /*, opts */)
129
- fastify.get('/', (req, reply) => {
130
- const md = reply.markdown(`**BOLD**`)
131
- reply.send(md)
132
- })
133
- fastify.inject({
134
- url: '/',
135
- method: 'GET'
136
- }, (err, res) => {
137
- t.error(err)
138
- t.strictEqual(res.payload.trim(), `<p><strong>BOLD</strong></p>`)
139
- })
140
- })
141
-
142
- test('Should markdown with "opts.data" and "markdown" method with "md" param the result is', t => {
143
- t.plan(2)
144
- const fastify = Fastify()
145
- fastify.register(plugin, {data: '**DATA**'})
146
- fastify.get('/', (req, reply) => {
147
- const md = reply.markdown(`**BOLD**`)
148
- reply.send(md)
149
- })
150
- fastify.inject({
151
- url: '/',
152
- method: 'GET'
153
- }, (err, res) => {
154
- t.error(err)
155
- t.strictEqual(res.payload.trim(), `<p><strong>BOLD</strong></p>`)
156
- })
157
- })
158
-
159
- test('Should markdown with "opts.data" and "opts.src" is', t => {
160
- t.plan(2)
161
- const fastify = Fastify()
162
- fastify.register(plugin, {src: path.join(__dirname, '..', 'Readme.md'), data: true})
163
- fastify.get('/', (req, reply) => {
164
- const md = reply.markdown(`**BOLD**`)
165
- reply.send(md)
166
- })
167
- fastify.inject({
168
- url: '/',
169
- method: 'GET'
170
- }, (err, res) => {
171
- t.error(err)
172
- t.strictEqual(res.payload.trim(), `<p><strong>BOLD</strong></p>`)
173
- })
174
- })
175
-
176
- test('Should markdown with "opts" but isnot [data,src,markedOptions] is', t => {
177
- t.plan(2)
178
- const fastify = Fastify()
179
- fastify.register(plugin, {anything: 'anything'})
180
- fastify.get('/', (req, reply) => {
181
- const md = reply.markdown().parse(`**BOLD**`)
182
- reply.send(md)
183
- })
184
- fastify.inject({
185
- url: '/',
186
- method: 'GET'
187
- }, (err, res) => {
188
- t.error(err)
189
- t.strictEqual(res.payload.trim(), `<p><strong>BOLD</strong></p>`)
190
- })
191
- })