Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 4, 2021
1 parent 499a17a commit 2110f19
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 129 deletions.
10 changes: 3 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict'
import {createCompiler} from './lib/compiler.js'

var compiler = require('./lib/compiler.js')

module.exports = man

function man(options) {
this.Compiler = compiler(options || {})
export default function remarkMan(options) {
this.Compiler = createCompiler(options || {})
}
67 changes: 30 additions & 37 deletions lib/compiler.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
'use strict'

var zwitch = require('zwitch')
var mapz = require('mapz')
var visit = require('unist-util-visit')
var toString = require('mdast-util-to-string')
var definitions = require('mdast-util-definitions')
var slugs = require('github-slugger')
var months = require('months')
var handlers = require('./handlers')
var escape = require('./escape')
var quote = require('./quote')
var macro = require('./macro')

module.exports = createCompiler
import zwitch from 'zwitch'
import mapz from 'mapz'
import visit from 'unist-util-visit'
import toString from 'mdast-util-to-string'
import definitions from 'mdast-util-definitions'
import slugs from 'github-slugger'
import months from 'months'
import {handlers} from './handlers.js'
import {escape} from './escape.js'
import {quote} from './quote.js'
import {macro} from './macro.js'

// Heading expressions.
var manExpression = /([\w_.[\]~+=@:-]+)\s*\((\d\w*)\)(?:\s*[-—–]+\s*(.*))?/
Expand All @@ -25,7 +21,7 @@ one.invalid = invalid
one.unknown = unknown
one.handlers = handlers

function createCompiler(defaults) {
export function createCompiler(defaults) {
Compiler.prototype.compile = compile
Compiler.prototype.one = one
Compiler.prototype.all = all
Expand All @@ -38,9 +34,8 @@ function createCompiler(defaults) {
}

function compile() {
var self = this
var tree = self.tree
var file = self.file
var tree = this.tree
var file = this.file
var slug = slugs()
var config = {}
var titles = 0
Expand All @@ -56,10 +51,22 @@ function createCompiler(defaults) {
this.headings = headings

// Check if there is one or more main headings.
visit(tree, 'heading', onheading)
visit(tree, 'heading', (node) => {
if (node.depth === 1) {
if (titles) {
this.increaseDepth = true
} else {
this.mainHeading = node
}

titles++
}

if (self.mainHeading) {
value = toString(self.mainHeading)
headings[slug.slug(toString(node))] = node
})

if (this.mainHeading) {
value = toString(this.mainHeading)
match = manExpression.exec(value)

if (match) {
Expand Down Expand Up @@ -97,7 +104,7 @@ function createCompiler(defaults) {

if (name) {
result +=
macro('SH', quote('NAME')) + '\n' + handlers.strong.call(self, name)
macro('SH', quote('NAME')) + '\n' + handlers.strong.call(this, name)
}

result += escape(name && description ? ' - ' + description : description)
Expand All @@ -110,20 +117,6 @@ function createCompiler(defaults) {
}

return result

function onheading(node) {
if (node.depth === 1) {
if (titles) {
self.increaseDepth = true
} else {
self.mainHeading = node
}

titles++
}

headings[slug.slug(toString(node))] = node
}
}
}

Expand Down
12 changes: 4 additions & 8 deletions lib/escape.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
'use strict'

var groff = require('groff-escape')

module.exports = escape
import {groffEscape} from 'groff-escape'

var escapes = {
'\\': 'rs',
Expand All @@ -13,7 +9,7 @@ var escapes = {
var expression = init()

// Escape a value for roff output.
function escape(value) {
export function escape(value) {
return value.replace(expression, replace)

function replace($0) {
Expand All @@ -26,9 +22,9 @@ function init() {
var keys = ['\\\\', '\\[', '\\]']
var key

for (key in groff) {
for (key in groffEscape) {
keys.push(key)
escapes[key] = groff[key]
escapes[key] = groffEscape[key]
}

return new RegExp(keys.sort(longest).join('|'), 'g')
Expand Down
84 changes: 40 additions & 44 deletions lib/handlers.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
'use strict'

var toString = require('mdast-util-to-string')
var escape = require('./escape')
var quote = require('./quote')
var macro = require('./macro')

exports.inlineCode = inlineCode
exports.strong = bold
exports.emphasis = italic
exports.delete = italic
exports.break = hardBreak
exports.link = link
exports.image = link
exports.heading = heading
exports.root = root
exports.paragraph = paragraph
exports.thematicBreak = rule
exports.blockquote = blockquote
exports.code = code
exports.list = list
exports.listItem = listItem
exports.text = text
exports.escape = text // To do: remove next major
exports.linkReference = reference
exports.imageReference = reference
exports.table = table
exports.definition = noop
import toString from 'mdast-util-to-string'
import {escape} from './escape.js'
import {quote} from './quote.js'
import {macro} from './macro.js'

export const handlers = {
inlineCode,
strong: bold,
emphasis: italic,
delete: italic,
break: hardBreak,
link,
image: link,
heading,
root,
paragraph,
thematicBreak: rule,
blockquote,
code,
list,
listItem,
text,
escape: text, // To do: remove next major
linkReference: reference,
imageReference: reference,
table,
definition: noop
}

var mailto = 'mailto:'
var p = macro('P', '\n')
Expand Down Expand Up @@ -97,16 +97,15 @@ function heading(node) {
}

function link(node, href) {
var self = this
var value = 'children' in node ? self.all(node).join('') : node.alt
var value = 'children' in node ? this.all(node).join('') : node.alt
var url = escape(typeof href === 'string' ? href : node.url || '')
var head

if (url && url.slice(0, mailto.length) === mailto) {
url = url.slice(mailto.length)
}

head = url.charAt(0) === '#' && self.headings[url.slice(1)]
head = url.charAt(0) === '#' && this.headings[url.slice(1)]

if (head) {
url = '(' + escape(toString(head)) + ')'
Expand All @@ -121,14 +120,14 @@ function link(node, href) {
}

if (value) {
value = bold.call(self, value)
value = bold.call(this, value)

if (url) {
value += ' '
}
}

return value + (url ? italic.call(self, url) : '')
return value + (url ? italic.call(this, url) : '')
}

function reference(node) {
Expand All @@ -143,16 +142,15 @@ function code(node) {
}

function blockquote(node) {
var self = this
var value

self.level++
this.level++

value = self.all(node).join('\n')
value = this.all(node).join('\n')

self.level--
this.level--

value = '.RS ' + (self.level ? 4 : 0) + '\n' + value + '\n.RE 0\n'
value = '.RS ' + (this.level ? 4 : 0) + '\n' + value + '\n.RE 0\n'

return value
}
Expand All @@ -162,22 +160,21 @@ function text(node) {
}

function list(node) {
var self = this
var start = node.start
var children = node.children
var length = children.length
var index = -1
var values = []
var bullet

self.level++
this.level++

while (++index < length) {
bullet = start ? start + index + '.' : '\\(bu'
values.push(listItem.call(self, children[index], bullet, index))
values.push(listItem.call(this, children[index], bullet, index))
}

self.level--
this.level--

return ['.RS ' + (this.level ? 4 : 0), values.join('\n'), '.RE 0\n'].join(
'\n'
Expand All @@ -191,7 +188,6 @@ function listItem(node, bullet) {
}

function table(node) {
var self = this
var rows = node.children
var index = rows.length
var align = node.align
Expand All @@ -209,7 +205,7 @@ function table(node) {
out = []

while (++pos < alignLength) {
out[pos] = self.all(row[pos]).join('')
out[pos] = this.all(row[pos]).join('')
}

result[index] = out.join('@')
Expand Down
6 changes: 1 addition & 5 deletions lib/macro.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict'

module.exports = macro

// Compile a roff macro.
function macro(name, value) {
export function macro(name, value) {
var clean = value || ''

if (clean && clean.charAt(0) !== '\n') {
Expand Down
6 changes: 1 addition & 5 deletions lib/quote.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
'use strict'

module.exports = quote

// Wrap `value` with double quotes, and escape internal double quotes.
function quote(value) {
export function quote(value) {
return '"' + String(value).replace(/"/g, '\\"') + '"'
}
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"lib",
"lib/",
"index.js"
],
"dependencies": {
"github-slugger": "^1.0.0",
"groff-escape": "^1.0.0",
"groff-escape": "^2.0.0",
"mapz": "^1.0.0",
"mdast-util-definitions": "^4.0.0",
"mdast-util-to-string": "^1.0.0",
Expand All @@ -54,7 +57,7 @@
"tape": "^5.0.0",
"unified": "^9.0.0",
"vfile": "^4.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 @@ -72,8 +75,9 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"unicorn/prefer-default-parameters": "off",
"unicorn/prefer-optional-catch-binding": "off",
"complexity": "off",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/overwrite.9/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"date": "2014-09-09",
"version": "0.0.1",
"manual": "The Manual"
}
}
Loading

0 comments on commit 2110f19

Please sign in to comment.