Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 13, 2020
0 parents commit 6039703
Show file tree
Hide file tree
Showing 16 changed files with 704 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage/
*.html
*.json
*.md
1 change: 1 addition & 0 deletions .remarkignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- lts/dubnium
- node
after_script: bash <(curl -s https://codecov.io/bash)
1 change: 1 addition & 0 deletions html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/html')
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/syntax')
10 changes: 10 additions & 0 deletions lib/html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
exports.enter = {strikethrough: onenterstrikethrough}
exports.exit = {strikethrough: onexitstrikethrough}

function onenterstrikethrough() {
this.tag('<del>')
}

function onexitstrikethrough() {
this.tag('</del>')
}
156 changes: 156 additions & 0 deletions lib/syntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
var classifyCharacter = require('micromark/dist/util/classify-character')
var shallow = require('micromark/dist/util/shallow')
var resolveAll = require('micromark/dist/util/resolve-all')

var characterGroupPunctuation = 2

var tokenizer = {
tokenize: tokenizeStrikethrough,
resolveAll: resolveAllStrikethrough
}

exports.text = {126: tokenizer}
exports.insideSpan = {null: tokenizer}

// Take events and resolve strikethrough.
function resolveAllStrikethrough(events, context) {
var length = events.length
var index = -1
var strikethrough
var opening
var closing
var text
var indexOpen
var eventsUpTo

// Walk through all events.
while (++index < length) {
closing = events[index][1]

// Find a token that can close.
if (closing.type === 'strikethroughSequenceTemporary' && closing._close) {
indexOpen = index

// Now walk back to find an opener.
while (indexOpen--) {
opening = events[indexOpen][1]

// Find a token that can open the closer.
if (
opening.type === 'strikethroughSequenceTemporary' &&
opening._open &&
closing.end.offset - closing.start.offset ===
opening.end.offset - opening.start.offset &&
!(closing._open || opening._close)
) {
closing.type = 'strikethroughSequence'
opening.type = 'strikethroughSequence'

strikethrough = {
type: 'strikethrough',
start: shallow(opening.start),
end: shallow(closing.end)
}

text = {
type: 'strikethroughText',
start: shallow(opening.end),
end: shallow(closing.start)
}

eventsUpTo = [].concat(
// Before.
events.slice(0, indexOpen - 1),
// Opening.
[
['enter', strikethrough, context],
['enter', opening, context],
['exit', opening, context],
['enter', text, context]
],
// Between.
resolveAll(
context.parser.constructs.insideSpan.null,
events.slice(indexOpen + 1, index),
context
),
// Closing.
[
['exit', text, context],
['enter', closing, context],
['exit', closing, context],
['exit', strikethrough, context]
]
)

// After.
events = eventsUpTo.concat(events.slice(index + 2))
length = events.length
index = eventsUpTo.length - 1
break
}
}
}
}

return removeRemainingSequences(events)
}

function removeRemainingSequences(events) {
var index = -1
var length = events.length

while (++index < length) {
if (events[index][1].type === 'strikethroughSequenceTemporary') {
events[index][1].type = 'data'
}
}

return events
}

function tokenizeStrikethrough(effects, ok, nok) {
var previous = this.previous
var events = this.events
var size = 0

return start

function start(code) {
if (
code !== 126 ||
(previous === 126 &&
events[events.length - 1][1].type !== 'characterEscape')
) {
return nok(code)
}

effects.enter('strikethroughSequenceTemporary')
return more(code)
}

function more(code) {
var token
var before
var after

if (code === 126) {
// If this is the third marker, exit.
if (size > 1) {
return nok(code)
}

effects.consume(code)
size++
return more
}

before = classifyCharacter(previous)
after = classifyCharacter(code)
token = effects.exit('strikethroughSequenceTemporary')
token._open = !after || (before && after === characterGroupPunctuation)
token._close = !before || (after && before === characterGroupPunctuation)

return ok(code)
}
}
22 changes: 22 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2020 Titus Wormer <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
75 changes: 75 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"name": "micromark-extension-gfm-strikethrough",
"version": "0.0.0",
"description": "micromark extension to support GFM strikethrough",
"license": "MIT",
"keywords": [
"micromark",
"micromark-extension",
"strikethrough",
"strike",
"through",
"del",
"delete",
"deletion",
"gfm",
"markdown",
"unified"
],
"repository": "micromark/micromark-extension-gfm-strikethrough",
"bugs": "https://github.com/micromark/micromark-extension-gfm-strikethrough/issues",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"author": "Titus Wormer <[email protected]> (https://wooorm.com)",
"contributors": [
"Titus Wormer <[email protected]> (https://wooorm.com)"
],
"files": [
"lib/",
"index.js",
"html.js"
],
"dependencies": {
"fault": "^1.0.0",
"micromark": "^2.3.0"
},
"devDependencies": {
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark-cli": "^8.0.0",
"remark-preset-wooorm": "^7.0.0",
"tape": "^5.0.0",
"xo": "^0.33.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test": "npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"prettier": true,
"esnext": false
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
}
}
Loading

0 comments on commit 6039703

Please sign in to comment.