Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 5, 2021
1 parent 7c82195 commit 2e5e5b1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 42 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from 'mdast-util-frontmatter'

export default function remarkFrontmatter(options) {
var data = this.data()
const data = this.data()
add('micromarkExtensions', frontmatter(options))
add('fromMarkdownExtensions', frontmatterFromMarkdown(options))
add('toMarkdownExtensions', frontmatterToMarkdown(options))
Expand Down
12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"tape": "^5.0.0",
"to-vfile": "^7.0.0",
"unified": "^10.0.0",
"xo": "^0.37.0"
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo --ignore-pattern test/ && prettier . -w --loglevel warn && xo --fix",
Expand All @@ -64,22 +64,14 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"unicorn/no-array-callback-reference": "off",
"unicorn/prefer-optional-catch-binding": "off",
"guard-for-in": "off"
},
"ignores": [
"types/"
]
},
"remarkConfig": {
"plugins": [
[
"./",
"./index.js",
[
"yaml",
"toml"
Expand Down
60 changes: 29 additions & 31 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@ import {remark} from 'remark'
import {isHidden} from 'is-hidden'
import remarkFrontmatter from '../index.js'

var join = path.join
var read = fs.readFileSync
var write = fs.writeFileSync
var dir = fs.readdirSync
const join = path.join
const read = fs.readFileSync
const write = fs.writeFileSync
const dir = fs.readdirSync

test('remarkFrontmatter', function (t) {
t.doesNotThrow(function () {
test('remarkFrontmatter', (t) => {
t.doesNotThrow(() => {
remark().use(remarkFrontmatter).freeze()
}, 'should not throw if not passed options')

t.doesNotThrow(function () {
t.doesNotThrow(() => {
unified().use(remarkFrontmatter).freeze()
}, 'should not throw if without parser or compiler')

t.throws(
function () {
() => {
unified().use(remarkFrontmatter, [1]).freeze()
},
/^Error: Expected matter to be an object, not `1`/,
'should throw if not given a preset or a matter'
)

t.throws(
function () {
() => {
unified().use(remarkFrontmatter, ['jsonml']).freeze()
},
/^Error: Missing matter definition for `jsonml`/,
'should throw if given an unknown preset'
)

t.throws(
function () {
() => {
unified()
.use(remarkFrontmatter, [{marker: '*'}])
.freeze()
Expand All @@ -48,7 +48,7 @@ test('remarkFrontmatter', function (t) {
)

t.throws(
function () {
() => {
unified()
.use(remarkFrontmatter, [{type: 'jsonml'}])
.freeze()
Expand All @@ -60,41 +60,39 @@ test('remarkFrontmatter', function (t) {
t.end()
})

test('fixtures', function (t) {
var base = join('test', 'fixtures')
var entries = dir(base).filter((d) => !isHidden(d))
test('fixtures', (t) => {
const base = join('test', 'fixtures')
const entries = dir(base).filter((d) => !isHidden(d))
let index = -1

t.plan(entries.length)

entries.forEach(each)

function each(fixture) {
t.test(fixture, function (st) {
var input = readSync(join(base, fixture, 'input.md'))
var treePath = join(base, fixture, 'tree.json')
var outputPath = join(base, fixture, 'output.md')
var output
var actual
var expected
var config
var proc
while (++index < entries.length) {
const fixture = entries[index]
t.test(fixture, (st) => {
const input = readSync(join(base, fixture, 'input.md'))
const treePath = join(base, fixture, 'tree.json')
const outputPath = join(base, fixture, 'output.md')
let output
let expected
let config

try {
config = JSON.parse(read(join(base, fixture, 'config.json')))
} catch (_) {}
} catch {}

proc = remark().use(remarkFrontmatter, config)
actual = JSON.parse(JSON.stringify(proc.parse(input)))
const proc = remark().use(remarkFrontmatter, config)
const actual = JSON.parse(JSON.stringify(proc.parse(input)))

try {
output = read(outputPath, 'utf8')
} catch (_) {
} catch {
output = String(input)
}

try {
expected = JSON.parse(read(treePath))
} catch (_) {
} catch {
// New fixture.
write(treePath, JSON.stringify(actual, 0, 2) + '\n')
expected = actual
Expand Down

0 comments on commit 2e5e5b1

Please sign in to comment.