bajo-markdown 1.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Ardhi Lukianto
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 ADDED
@@ -0,0 +1,27 @@
1
+ # bajo-markdown
2
+
3
+ Plugin name: **bajoMarkdown**, alias: **md**
4
+
5
+ ![GitHub package.json version](https://img.shields.io/github/package-json/v/ardhi/bajo-markdown) ![NPM Version](https://img.shields.io/npm/v/bajo-markdown)
6
+
7
+ > <br />**Attention**: I do NOT accept any pull request at the moment, thanks!<br /><br />
8
+
9
+ Markdown support for [Bajo](https://github.com/ardhi/bajo).
10
+
11
+ ## Installation
12
+
13
+ Goto your ```<bajo-base-dir>``` and type:
14
+
15
+ ```bash
16
+ $ npm install bajo-markdown
17
+ ```
18
+
19
+ Now open your ```<bajo-data-dir>/config/.plugins``` and put ```bajo-markdown``` in it
20
+
21
+ ## Class Method
22
+
23
+ - ```function parse(text)```
24
+
25
+ ## License
26
+
27
+ [MIT](LICENSE)
package/bajo/.alias ADDED
@@ -0,0 +1 @@
1
+ md
@@ -0,0 +1,8 @@
1
+ {
2
+ "renderer": {
3
+ "tableClass": "table my-3",
4
+ "tableHeadClass": "",
5
+ "tableBodyClass": "table-group-divider"
6
+ },
7
+ "dependencies": ["bajo-config"]
8
+ }
@@ -0,0 +1,9 @@
1
+ function parseContent (input, options = {}) {
2
+ let html = this.instance.parse(input)
3
+ html = this.unescapeBlock(html, '&lt;&lt;c:', '&gt;&gt;')
4
+ html = this.unescapeBlock(html, '&lt;&lt;/c:', '&gt;&gt;')
5
+ html = this.unescapeBlock(html, '&lt;&lt;%', '%&gt;&gt;') // lodash template
6
+ return html
7
+ }
8
+
9
+ export default parseContent
@@ -0,0 +1,18 @@
1
+ function parseFrontMatter (input) {
2
+ const { isPlainObject, isArray } = this.app.bajo.lib._
3
+ const { fromYaml, fromToml, fromJson } = this.app.bajoConfig
4
+
5
+ const handlers = []
6
+ handlers.push(fromYaml, fromToml, fromJson)
7
+ let success
8
+ for (const handler of handlers) {
9
+ if (success) break
10
+ try {
11
+ const result = handler(input, true)
12
+ if (isPlainObject(result) || isArray(result)) success = result
13
+ } catch (err) {}
14
+ }
15
+ return success ?? {}
16
+ }
17
+
18
+ export default parseFrontMatter
@@ -0,0 +1,15 @@
1
+ function parse (input, opts = {}) {
2
+ const { omit, pick } = this.app.bajo.lib._
3
+ opts.parseContent = opts.parseContent ?? true
4
+
5
+ let { frontMatter, content } = this.split(input, pick(opts, ['readFile']))
6
+ frontMatter = this.parseFrontMatter(frontMatter)
7
+ const html = opts.parseContent ? this.parseContent(content, omit(opts, ['parseContent', 'readFile'])) : undefined
8
+ return {
9
+ frontMatter,
10
+ html,
11
+ content
12
+ }
13
+ }
14
+
15
+ export default parse
@@ -0,0 +1,21 @@
1
+ const start = '---\n'
2
+ const end = '\n---'
3
+
4
+ function split (input, { readFile } = {}) {
5
+ const { fs } = this.app.bajo.lib
6
+ let content = readFile ? fs.readFileSync(input, 'utf8') : input
7
+ let text = content.replaceAll('\r\n', '\n')
8
+ const open = text.indexOf(start)
9
+ let frontMatter
10
+ if (open > -1) {
11
+ text = text.slice(open + end.length)
12
+ const close = text.indexOf(end)
13
+ if (close > -1) {
14
+ frontMatter = text.slice(0, close)
15
+ content = text.slice(close + end.length)
16
+ }
17
+ }
18
+ return { frontMatter, content }
19
+ }
20
+
21
+ export default split
@@ -0,0 +1,16 @@
1
+ function unescape (text) {
2
+ if (this.app.waibu) return this.app.waibu.unescape(text)
3
+ return text.replaceAll('&lt;', '<').replaceAll('&gt;', '>')
4
+ }
5
+
6
+ function unescapeBlock (content, start, end) {
7
+ const { sliceString } = this.app.bajo
8
+ const sliced = sliceString(content, start, end)
9
+ if (!sliced) return content
10
+ const unescaped = unescape.call(this, sliced)
11
+ const replacer = unescaped.slice(1, unescaped.length - 1)
12
+ const result = content.replaceAll(sliced, replacer)
13
+ return unescapeBlock.call(this, result, start, end)
14
+ }
15
+
16
+ export default unescapeBlock
package/bajo/start.js ADDED
@@ -0,0 +1,38 @@
1
+ import { Marked } from 'marked'
2
+ import { markedHighlight } from 'marked-highlight'
3
+ import customHeadingId from 'marked-custom-heading-id'
4
+ import { mangle } from 'marked-mangle'
5
+ import katex from 'marked-katex-extension'
6
+ import { emoji } from '../lib/emoji.js'
7
+ import Renderer from '../lib/renderer.js'
8
+
9
+ async function start () {
10
+ const { importPkg } = this.app.bajo
11
+ const renderer = await Renderer.call(this)
12
+ const options = [
13
+ customHeadingId(),
14
+ mangle(),
15
+ katex({ throwOnError: false })
16
+ ]
17
+ if (this.app.waibuExtra) {
18
+ const hljs = await importPkg('waibuExtra:highlight.js')
19
+ const highlight = markedHighlight({
20
+ langPrefix: 'hljs language-',
21
+ highlight (code, lang) {
22
+ const language = hljs.getLanguage(lang) ? lang : 'plaintext'
23
+ return hljs.highlight(code, { language }).value
24
+ }
25
+ })
26
+ options.push(highlight)
27
+ }
28
+ const marked = new Marked(options)
29
+ marked.use({ renderer, extensions: [emoji] })
30
+ marked.Renderer.prototype.paragraph = (text) => {
31
+ if (text.startsWith('&lt;c:')) return text + '\n' // for weibu component
32
+ return '<p>' + text + '</p>'
33
+ }
34
+
35
+ this.instance = marked
36
+ }
37
+
38
+ export default start
package/lib/emoji.js ADDED
@@ -0,0 +1,24 @@
1
+ // based on: https://github.com/rinzool/marked-twemoji/blob/main/index.js
2
+ import * as nodeEmoji from 'node-emoji'
3
+
4
+ export const emoji = {
5
+ name: 'emoji',
6
+ level: 'inline',
7
+ start (src) {
8
+ return src.indexOf(':')
9
+ },
10
+ tokenizer (src, _) {
11
+ const rule = /^:(\w+):/
12
+ const match = rule.exec(src)
13
+ if (match) {
14
+ return {
15
+ type: 'emoji',
16
+ raw: match[0],
17
+ emoji: match[1]
18
+ }
19
+ }
20
+ },
21
+ renderer (token) {
22
+ return nodeEmoji.emojify(token.raw)
23
+ }
24
+ }
@@ -0,0 +1,15 @@
1
+ async function renderer () {
2
+ const config = this.config
3
+ return {
4
+ table (header, body) {
5
+ return `
6
+ <table class="${config.renderer.tableClass}">
7
+ <thead class="${config.renderer.tableHeadClass}">${header}</thead>
8
+ <tbody class="${config.renderer.tableBodyClass}">${body}</tbody>
9
+ </table>
10
+ `
11
+ }
12
+ }
13
+ }
14
+
15
+ export default renderer
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "bajo-markdown",
3
+ "version": "1.0.1",
4
+ "description": "Markdown support for Bajo Framework",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "type": "module",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/ardhi/bajo-markdown.git"
13
+ },
14
+ "keywords": [
15
+ "markdown",
16
+ "bajo",
17
+ "framework",
18
+ "fastify",
19
+ "modular"
20
+ ],
21
+ "author": "Ardhi Lukianto <ardhi@lukianto.com>",
22
+ "license": "MIT",
23
+ "bugs": {
24
+ "url": "https://github.com/ardhi/bajo-markdown/issues"
25
+ },
26
+ "homepage": "https://github.com/ardhi/bajo-markdown#readme",
27
+ "dependencies": {
28
+ "gray-matter": "^4.0.3",
29
+ "marked": "^15.0.6",
30
+ "marked-custom-heading-id": "^2.0.11",
31
+ "marked-extended-tables": "^1.1.0",
32
+ "marked-highlight": "^2.2.1",
33
+ "marked-katex-extension": "^5.1.4",
34
+ "marked-mangle": "^1.1.10",
35
+ "node-emoji": "^2.2.0"
36
+ }
37
+ }