fastify-markdown 0.5.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/Readme.md CHANGED
@@ -24,7 +24,7 @@ const fastify = require('fastify')({ logger: { level: 'trace' } })
24
24
  fastify
25
25
  .register(require('fastify-markdown'), { src: true })
26
26
  .get('/', (req, reply) => {
27
- return reply.markdown(path.join(__dirname, '..', 'Readme.md'))
27
+ return reply.markdown(path.join(__dirname, 'Readme.md'))
28
28
  })
29
29
  .listen({ port: 3000 }, err => {
30
30
  if (err) throw err
@@ -37,7 +37,7 @@ fastify
37
37
  * async using opts.src from file
38
38
  */
39
39
  fastify
40
- .register(require('../'), {
40
+ .register(require('fastify-markdown'), {
41
41
  src: true, markedOptions: { gfm: false }
42
42
  })
43
43
  .get('/', async (req, reply) => {
package/index.js CHANGED
@@ -7,30 +7,28 @@ const { marked } = require('marked')
7
7
  const fp = require('fastify-plugin')
8
8
 
9
9
  function none (obj) {
10
- if (obj === undefined) return true
11
- if (obj && Object.getOwnPropertyNames(obj).length === 0) return true
10
+ if (obj == null) return true
11
+ if (typeof obj === 'object' && Object.getOwnPropertyNames(obj).length === 0) return true
12
12
  return false
13
13
  }
14
14
 
15
- function have (obj) {
16
- return !!obj
17
- }
18
-
19
15
  function isString (str) {
20
16
  return typeof str === 'string'
21
17
  }
22
18
 
23
19
  function asyncFileMarked (src, option) {
24
20
  const read = util.promisify(fs.readFile)
25
- return read(src, 'utf8').then(data => marked.parse(data, option), err => err)
21
+ return read(src, 'utf8').then(data => marked.parse(data, option))
26
22
  }
27
23
 
28
24
  async function fastifyMarkdown (fastify, opts) {
29
25
  const markedOptions = opts.markedOptions || {}
30
26
 
31
27
  fastify.decorateReply('markdown', function (md) {
32
- if (none(opts) && none(md)) return marked
33
- if (none(opts) && have(md)) opts = { data: md }
28
+ if (none(opts)) {
29
+ if (none(md)) return marked
30
+ return marked.parse(md, markedOptions)
31
+ }
34
32
 
35
33
  if (opts.data) {
36
34
  if (none(md) && isString(opts.data)) md = opts.data
@@ -40,9 +38,8 @@ async function fastifyMarkdown (fastify, opts) {
40
38
  return asyncFileMarked(md, markedOptions)
41
39
  } else if (opts.markedOptions) {
42
40
  return marked.setOptions(markedOptions)
43
- } else {
44
- return marked
45
41
  }
42
+ return marked
46
43
  })
47
44
  }
48
45
 
package/package.json CHANGED
@@ -1,8 +1,15 @@
1
1
  {
2
2
  "name": "fastify-markdown",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Plugin for markdown parser",
5
5
  "main": "index.js",
6
+ "types": "./fastify/index.d.ts",
7
+ "files": [
8
+ "index.js",
9
+ "fastify/",
10
+ "LICENSE",
11
+ "Readme.md"
12
+ ],
6
13
  "scripts": {
7
14
  "test": "npm run lint && npm run unit",
8
15
  "lint": "standard | snazzy",
@@ -36,6 +43,5 @@
36
43
  "snazzy": "^9.0.0",
37
44
  "standard": "^17.1.2",
38
45
  "tap": "^21.7.5"
39
- },
40
- "types": "./fastify/index.d.ts"
46
+ }
41
47
  }
@@ -1,23 +0,0 @@
1
- name: CI
2
-
3
- on:
4
- push:
5
- branches: [master]
6
- pull_request:
7
- branches: [master]
8
-
9
- jobs:
10
- test:
11
- runs-on: ubuntu-latest
12
- strategy:
13
- matrix:
14
- node-version: [20, 22, 24]
15
- steps:
16
- - uses: actions/checkout@v4
17
- - name: Use Node.js ${{ matrix.node-version }}
18
- uses: actions/setup-node@v4
19
- with:
20
- node-version: ${{ matrix.node-version }}
21
- cache: npm
22
- - run: npm ci
23
- - run: npm test
package/example/server.js DELETED
@@ -1,71 +0,0 @@
1
- 'use strict'
2
-
3
- const path = require('path')
4
- const fastify = require('fastify')({
5
- logger: {
6
- level: 'trace'
7
- }
8
- })
9
-
10
- /**
11
- * using opts.src = 'file'
12
- */
13
- // fastify
14
- // .register(require('../'), {
15
- // src: true, markedOptions: { gfm: false }
16
- // })
17
- // .get('/', (req, reply) => {
18
- // return reply.markdown(path.join(__dirname, '..', 'Readme.md'))
19
- // })
20
- // .listen({ port: 3000 })
21
- // .then(() => console.log('server running on http://localhost:3000 ...'))
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
-
34
- /**
35
- * using opts.data = true
36
- */
37
- // fastify
38
- // .register(require('../'), {
39
- // data: true, markedOptions: { gfm: false }
40
- // })
41
- // .get('/', (req, reply) => {
42
- // const md = reply.markdown('**BOLD**')
43
- // reply.send(md)
44
- // })
45
-
46
- /**
47
- * default marked data
48
- */
49
- // fastify
50
- // .register(require('../'))
51
- // .get('/', (req, reply) => {
52
- // const md = reply.markdown('**BOLD**')
53
- // reply.send(md)
54
- // })
55
-
56
- /**
57
- * using internal marked and options
58
- */
59
- // fastify
60
- // .register(require('../'))
61
- // .get('/', (req, reply) => {
62
- // const md = reply.markdown().parse('**BOLD**', { breaks: true })
63
- // reply.send(md)
64
- // })
65
-
66
- fastify.listen({ port: 3000 })
67
- .then(() => console.log('server running on http://localhost:3000 ...'))
68
- .catch(err => {
69
- fastify.log.error(err)
70
- process.exit(1)
71
- })
@@ -1,171 +0,0 @@
1
- 'use strict'
2
-
3
- const path = require('path')
4
- const { test } = require('tap')
5
- const Fastify = require('fastify')
6
- const plugin = require('../')
7
-
8
- test('Should markdown with "opts.data" is', async t => {
9
- const fastify = Fastify()
10
- fastify.register(plugin, {
11
- data: '**BOLD**'
12
- })
13
- fastify.get('/', (req, reply) => {
14
- const md = reply.markdown()
15
- reply.send(md)
16
- })
17
- const res = await fastify.inject({
18
- url: '/',
19
- method: 'GET'
20
- })
21
- t.error(res.error)
22
- t.equal(res.payload.trim(), '<p><strong>BOLD</strong></p>')
23
- })
24
-
25
- test('Should markdown with "opts.data" and "opts.markedOptions" is', async t => {
26
- const fastify = Fastify()
27
- fastify.register(plugin, {
28
- data: true, markedOptions: { gfm: false }
29
- })
30
- fastify.get('/', (req, reply) => {
31
- const md = reply.markdown('**BOLD**')
32
- reply.send(md)
33
- })
34
- const res = await fastify.inject({
35
- url: '/',
36
- method: 'GET'
37
- })
38
- t.error(res.error)
39
- t.equal(res.payload, '<p><strong>BOLD</strong></p>\n')
40
- })
41
-
42
- test('Should markdown with "opts.src" is', async t => {
43
- const fastify = Fastify()
44
- fastify.register(plugin, {
45
- src: true
46
- })
47
- fastify.get('/', (req, reply) => {
48
- return reply.markdown(path.join(__dirname, '..', 'Readme.md'))
49
- })
50
- const res = await fastify.inject({
51
- url: '/',
52
- method: 'GET'
53
- })
54
- t.error(res.error)
55
- t.ok(res.payload)
56
- })
57
-
58
- test('Should markdown with "opts.src" is incorrect', async t => {
59
- const fastify = Fastify()
60
- fastify.register(plugin, {
61
- src: 'empty'
62
- })
63
- fastify.get('/', (req, reply) => {
64
- reply.markdown().then(md => {
65
- reply.send(md)
66
- }).catch(err => {
67
- reply.send(err)
68
- })
69
- })
70
- const res = await fastify.inject({
71
- url: '/',
72
- method: 'GET'
73
- })
74
- t.error(res.error)
75
- t.equal(JSON.parse(res.payload).statusCode, 500)
76
- })
77
-
78
- test('Should markdown with "opts.markedOptions" is', async t => {
79
- const testOptions = {
80
- gfm: false
81
- }
82
- const fastify = Fastify()
83
- fastify.register(plugin, {
84
- markedOptions: testOptions
85
- })
86
- fastify.get('/', (req, reply) => {
87
- const gfm = reply.markdown().defaults.gfm
88
- reply.send(gfm)
89
- })
90
- const res = await fastify.inject({
91
- url: '/',
92
- method: 'GET'
93
- })
94
- t.error(res.error)
95
- t.equal(res.payload, 'false')
96
- })
97
-
98
- test('Should markdown without "opts" is', async t => {
99
- const fastify = Fastify()
100
- fastify.register(plugin /*, opts */)
101
- fastify.get('/', (req, reply) => {
102
- const md = reply.markdown().parse('**BOLD**')
103
- reply.send(md)
104
- })
105
- const res = await fastify.inject({
106
- url: '/',
107
- method: 'GET'
108
- })
109
- t.error(res.error)
110
- t.equal(res.payload.trim(), '<p><strong>BOLD</strong></p>')
111
- })
112
-
113
- test('Should markdown without "opts" and "markdown" method with "md" param is', async t => {
114
- const fastify = Fastify()
115
- fastify.register(plugin /*, opts */)
116
- fastify.get('/', (req, reply) => {
117
- const md = reply.markdown('**BOLD**')
118
- reply.send(md)
119
- })
120
- const res = await fastify.inject({
121
- url: '/',
122
- method: 'GET'
123
- })
124
- t.error(res.error)
125
- t.equal(res.payload.trim(), '<p><strong>BOLD</strong></p>')
126
- })
127
-
128
- test('Should markdown with "opts.data" and "markdown" method with "md" param the result is', async t => {
129
- const fastify = Fastify()
130
- fastify.register(plugin, { data: '**DATA**' })
131
- fastify.get('/', (req, reply) => {
132
- const md = reply.markdown('**BOLD**')
133
- reply.send(md)
134
- })
135
- const res = await fastify.inject({
136
- url: '/',
137
- method: 'GET'
138
- })
139
- t.error(res.error)
140
- t.equal(res.payload.trim(), '<p><strong>BOLD</strong></p>')
141
- })
142
-
143
- test('Should markdown with "opts.data" and "opts.src" is', async t => {
144
- const fastify = Fastify()
145
- fastify.register(plugin, { src: path.join(__dirname, '..', 'Readme.md'), data: true })
146
- fastify.get('/', (req, reply) => {
147
- const md = reply.markdown('**BOLD**')
148
- reply.send(md)
149
- })
150
- const res = await fastify.inject({
151
- url: '/',
152
- method: 'GET'
153
- })
154
- t.error(res.error)
155
- t.equal(res.payload.trim(), '<p><strong>BOLD</strong></p>')
156
- })
157
-
158
- test('Should markdown with "opts" but isnot [data,src,markedOptions] is', async t => {
159
- const fastify = Fastify()
160
- fastify.register(plugin, { anything: 'anything' })
161
- fastify.get('/', (req, reply) => {
162
- const md = reply.markdown().parse('**BOLD**')
163
- reply.send(md)
164
- })
165
- const res = await fastify.inject({
166
- url: '/',
167
- method: 'GET'
168
- })
169
- t.error(res.error)
170
- t.equal(res.payload.trim(), '<p><strong>BOLD</strong></p>')
171
- })