Initial commit

This commit is contained in:
Sol Fisher Romanoff 2022-07-08 11:32:20 +03:00
commit 003d398450
No known key found for this signature in database
GPG key ID: 9D3F2B64F2341B62
10 changed files with 10840 additions and 0 deletions

9
.editorconfig Normal file
View file

@ -0,0 +1,9 @@
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

13
.eslintrc.cjs Normal file
View file

@ -0,0 +1,13 @@
module.exports = {
root: true,
plugins: ['jest'],
extends: ['standard'],
rules: {
'comma-dangle': ['error', 'always-multiline'],
'arrow-parens': 0,
'no-tabs': 0,
},
env: {
'jest/globals': true,
},
}

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules/

55
LICENSE Normal file
View file

@ -0,0 +1,55 @@
# Blue Oak Model License
Version 1.0.0
## Purpose
This license gives everyone as much permission to work with
this software as possible, while protecting contributors
from liability.
## Acceptance
In order to receive this license, you must agree to its
rules. The rules of this license are both obligations
under that agreement and conditions to your license.
You must not do anything with this software that triggers
a rule that you cannot or will not follow.
## Copyright
Each contributor licenses you to do everything with this
software that would otherwise infringe that contributor's
copyright in it.
## Notices
You must ensure that everyone who gets a copy of
any part of this software from you, with or without
changes, also gets the text of this license or a link to
<https://blueoakcouncil.org/license/1.0.0>.
## Excuse
If anyone notifies you in writing that you have not
complied with [Notices](#notices), you can keep your
license by taking all practical steps to comply within 30
days after the notice. If you do not do so, your license
ends immediately.
## Patent
Each contributor licenses you to do everything with this
software that would otherwise infringe any patent claims
they can license or become able to license.
## Reliability
No contributor can revoke this license.
## No Liability
***As far as the law allows, this software comes as is,
without any warranty or condition, and no contributor
will be liable to anyone for any damages related to this
software or this license, under any kind of legal claim.***

3
babel.config.json Normal file
View file

@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}

20
jest.config.js Normal file
View file

@ -0,0 +1,20 @@
export default {
restoreMocks: true,
clearMocks: true,
collectCoverageFrom: [
'src/index.js',
],
coverageDirectory: 'coverage',
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
testRegex: /\.test\.js$/.source,
transform: {
'\\.[jt]sx?$': 'babel-jest',
},
}

10648
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

33
package.json Normal file
View file

@ -0,0 +1,33 @@
{
"name": "marked-mfm",
"version": "0.1.0",
"description": "Marked.js extension for Misskey-flavored Markdown",
"main": "./src/index.js",
"type": "module",
"scripts": {
"lint": "eslint .",
"lint-fix": "eslint --fix .",
"test": "jest --verbose"
},
"repository": {
"type": "git",
"url": "https://akkoma.dev/sfr/marked-mfm"
},
"bugs": {
"url": "https://akkoma.dev/sfr/marked-mfm/issues"
},
"author": "Sol Fisher Romanoff <sol@solfisher.com>",
"license": "BlueOak-1.0.0",
"peerDependencies": {
"marked": "^4.0.17"
},
"dependencies": {
"@babel/core": "^7.18.6",
"@babel/preset-env": "^7.18.6",
"babel-jest": "^28.1.2",
"eslint": "^8.19.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-jest": "^26.5.3",
"jest-cli": "^28.1.2"
}
}

40
src/index.js Normal file
View file

@ -0,0 +1,40 @@
export default (options = {}) => ({
extensions: [
{
name: 'mfm',
level: 'inline',
start(src) { return src.match(/\$\[/)?.index },
tokenizer (src, tokens) {
const rule = /^\$\[([\w\d]+) (.+)\]$/
const match = rule.exec(src)
if (match) {
const token = {
type: 'mfm',
raw: match[0],
markup: match[1],
text: match[2],
tokens: []
}
this.lexer.inline(token.text, token.tokens)
return token
}
},
renderer (token) {
const el = tokenClass => `<span style="display: inline-block" class="${tokenClass}">${this.parser.parseInline(token.tokens)}</span>`
switch (token.markup) {
case 'x2':
return el('x2')
break
case 'twitch':
return el('twitch')
break
case 'sparkle':
return el('sparkle')
break
default:
return token.raw
}
},
},
]
})

18
test/index.test.js Normal file
View file

@ -0,0 +1,18 @@
import { marked } from 'marked'
import markedMfm from '../src/index.js'
describe('marked-mfm', () => {
beforeEach(() => {
marked.setOptions(marked.getDefaults())
})
test('inline', () => {
marked.use(markedMfm())
expect(marked('$[x2 this text is bigger]')).toBe('<p><span style="display: inline-block" class="x2">this text is bigger</span></p>\n')
})
test('nested', () => {
marked.use(markedMfm())
expect(marked('$[twitch twitch $[sparkle sparkle]]')).toBe('<p><span style="display: inline-block" class="twitch">twitch <span style="display: inline-block" class="sparkle">sparkle</span></span></p>\n')
})
})