Skip to content

Commit

Permalink
Update dependencies, requiring ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Jun 30, 2024
1 parent c605788 commit 7b87cdb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 54 deletions.
16 changes: 8 additions & 8 deletions bin.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#!/usr/bin/env node

var args = require('minimist')(process.argv.slice(2))
var concat = require('simple-concat')
var insert = require('./')
var fs = require('fs')
const args = require('minimist')(process.argv.slice(2))
const concat = require('simple-concat')
const insert = require('./')
const fs = require('fs')

var file = args._[0]
const file = args._[0]
if (!file) {
console.error(fs.readFileSync(require.resolve('./usage.txt'), 'utf8'))
process.exit(1)
}

var inPlace = args.i || false
var source = fs.createReadStream(file)
const inPlace = args.i || false
const source = fs.createReadStream(file)
concat(process.stdin, function (err, content) {
if (err) throw err

var stream = insert({
const stream = insert({
content: content.toString('utf8'),
header: args.header,
region: args.region
Expand Down
34 changes: 17 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var Transform = require('stream').Transform
var inherits = require('util').inherits
var assert = require('assert')
var remark = require('remark')
var heading = require('mdast-util-heading-range')
var zone = require('mdast-zone')
var multisplice = require('multisplice')
import { Transform } from 'node:stream'
import { inherits } from 'node:util'
import assert from 'node:assert'
import { remark } from 'remark'
import { headingRange } from 'mdast-util-heading-range'
import { zone } from 'mdast-zone'
import multisplice from 'multisplice'

module.exports = InsertStream
export default InsertStream

function InsertStream (opts) {
if (!(this instanceof InsertStream)) return new InsertStream(opts)
Expand All @@ -28,25 +28,25 @@ InsertStream.prototype._transform = function (chunk, enc, next) {
}

InsertStream.prototype._flush = function (done) {
var stream = this
var opts = stream.opts
var newContent = opts.content
var tree = remark.parse(stream.buffer)
const stream = this
const opts = stream.opts
let newContent = opts.content
const tree = remark.parse(stream.buffer)

if (opts.header) {
heading(tree, opts.header, onrange)
headingRange(tree, opts.header, onrange)
} else {
zone(tree, opts.region, onrange)
}

function onrange (start, content, end) {
var s = start.position.end.offset + 1 // +1 for the newline after the header or the <!--comment-->
var lastParagraph = content[content.length - 1]
var e = lastParagraph ? lastParagraph.position.end.offset : (end ? end.position.start.offset : undefined)
const s = start.position.end.offset + 1 // +1 for the newline after the header or the <!--comment-->
const lastParagraph = content[content.length - 1]
const e = lastParagraph ? lastParagraph.position.end.offset : (end ? end.position.start.offset : undefined)

// if we have a bunch of whitespace after the paragraph in the original text,
// don't add another one even if the new content has a \n
var trailingWhitespace = stream.buffer.slice(
const trailingWhitespace = stream.buffer.slice(
lastParagraph ? lastParagraph.position.end.offset : stream.buffer.length,
end ? end.position.start.offset : undefined
)
Expand Down
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
"description": "insert content under a markdown header or into a comment delimited region",
"version": "1.0.1",
"author": "Renée Kooi <renee@kooi.me>",
"type": "module",
"bin": "bin.js",
"bugs": {
"url": "https://github.com/goto-bus-stop/md-insert/issues"
},
"dependencies": {
"mdast-util-heading-range": "^2.1.0",
"mdast-zone": "^4.0.0",
"mdast-util-heading-range": "^4.0.0",
"mdast-zone": "^6.1.0",
"minimist": "^1.2.0",
"multisplice": "^1.0.0",
"remark": "^12.0.1",
"remark": "^15.0.1",
"simple-concat": "^1.0.1"
},
"devDependencies": {
"depcheck": "^1.0.0",
"standard": "^17.0.0",
"tape": "^5.0.1"
"standard": "^17.1.0"
},
"homepage": "https://github.com/goto-bus-stop/md-insert",
"keywords": [
Expand All @@ -32,13 +32,17 @@
],
"license": "Apache-2.0",
"main": "index.js",
"exports": {
"./package.json": "./package.json",
".": "./index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/goto-bus-stop/md-insert.git"
},
"scripts": {
"lint": "standard && depcheck",
"tests-only": "node test",
"tests-only": "node --test",
"test": "npm run -s tests-only && npm run lint"
}
}
47 changes: 24 additions & 23 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,59 @@
var test = require('tape')
var concat = require('simple-concat')
var mdinsert = require('.')
import test from 'node:test'
import assert from 'node:assert'
import concat from 'simple-concat'
import mdinsert from 'md-insert'

test('header', function (t) {
var s = mdinsert({
test('header', function (t, done) {
const s = mdinsert({
header: 'Cool Things',
content: 'Interesting content'
})
concat(s, function (e, result) {
t.ifError(e)
t.equal(result + '', '# Title\n\n## Cool Things\nInteresting content\n\n# Uncool things\nUninteresting content\n')
t.end()
assert.ifError(e)
assert.equal(result + '', '# Title\n\n## Cool Things\nInteresting content\n\n# Uncool things\nUninteresting content\n')
done()
})

s.end('# Title\n\n## Cool Things\nblah blah\n\n# Uncool things\nUninteresting content\n')
})

test('header with trailing newline', function (t) {
var s = mdinsert({
test('header with trailing newline', function (t, done) {
const s = mdinsert({
header: 'Cool Things',
content: 'Interesting content\n'
})
concat(s, function (e, result) {
t.ifError(e)
t.equal(result + '', '# Title\n\n## Cool Things\nInteresting content\n\n# Uncool things\nUninteresting content\n')
t.end()
assert.ifError(e)
assert.equal(result + '', '# Title\n\n## Cool Things\nInteresting content\n\n# Uncool things\nUninteresting content\n')
done()
})

s.end('# Title\n\n## Cool Things\nblah blah\n\n# Uncool things\nUninteresting content\n')
})

test('region', function (t) {
var s = mdinsert({
test('region', function (t, done) {
const s = mdinsert({
region: 'insert',
content: 'Interesting content'
})
concat(s, function (e, result) {
t.ifError(e)
t.equal(result + '', '# Title\n\n## Cool Things\n<!--insert start-->\nInteresting content\n<!--insert end-->\n\n# Uncool things\nUninteresting content\n')
t.end()
assert.ifError(e)
assert.equal(result + '', '# Title\n\n## Cool Things\n<!--insert start-->\nInteresting content\n<!--insert end-->\n\n# Uncool things\nUninteresting content\n')
done()
})

s.end('# Title\n\n## Cool Things\n<!--insert start-->\nblah blah\n<!--insert end-->\n\n# Uncool things\nUninteresting content\n')
})

test('region with trailing newline', function (t) {
var s = mdinsert({
test('region with trailing newline', function (t, done) {
const s = mdinsert({
region: 'insert',
content: 'Interesting content\n'
})
concat(s, function (e, result) {
t.ifError(e)
t.equal(result + '', '# Title\n\n## Cool Things\n<!--insert start-->\nInteresting content\n\n<!--insert end-->\n\n# Uncool things\nUninteresting content\n')
t.end()
assert.ifError(e)
assert.equal(result + '', '# Title\n\n## Cool Things\n<!--insert start-->\nInteresting content\n\n<!--insert end-->\n\n# Uncool things\nUninteresting content\n')
done()
})

s.end('# Title\n\n## Cool Things\n<!--insert start-->\nblah blah\n<!--insert end-->\n\n# Uncool things\nUninteresting content\n')
Expand Down

0 comments on commit 7b87cdb

Please sign in to comment.