Skip to content

Commit

Permalink
Fix to remove empty paragraphs, funky tables
Browse files Browse the repository at this point in the history
* Refactor to improve bundle size
* Fix to remove empty texts from tree
* Fix to not add empty paragraphs
* Fix content in tables but outside of rows, cells
* Fix empty tables
* Fix potential IDs leaking into prototypes
  • Loading branch information
wooorm committed Oct 31, 2020
1 parent dce4ed0 commit 3c18027
Show file tree
Hide file tree
Showing 37 changed files with 378 additions and 367 deletions.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

module.exports = toMdast

var has = require('hast-util-has-property')
var minify = require('rehype-minify-whitespace')
var convert = require('unist-util-is/convert')
var visit = require('unist-util-visit')
var xtend = require('xtend')
var one = require('./lib/one')
var handlers = require('./lib/handlers')
var own = require('./lib/util/own')

var block = convert(['heading', 'paragraph', 'root'])

Expand All @@ -22,7 +24,7 @@ function toMdast(tree, options) {
h.wrapText = true
h.qNesting = 0

h.handlers = xtend(handlers, settings.handlers || {})
h.handlers = settings.handlers ? xtend(handlers, settings.handlers) : handlers
h.augment = augment

h.document = settings.document
Expand Down Expand Up @@ -74,9 +76,9 @@ function toMdast(tree, options) {
}

function onelement(node) {
var id = node.properties.id
var id = has(node, 'id') && String(node.properties.id).toUpperCase()

if (id && !(id in byId)) {
if (id && !own.call(byId, id)) {
byId[id] = node
}
}
Expand Down Expand Up @@ -115,5 +117,10 @@ function toMdast(tree, options) {
node.value = node.value.replace(/[\t ]+$/, '')
}
}

if (!node.value) {
parent.children.splice(index, 1)
return index
}
}
}
3 changes: 1 addition & 2 deletions lib/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ var one = require('./one')

function all(h, parent) {
var nodes = parent.children || []
var length = nodes.length
var values = []
var index = -1
var result

while (++index < length) {
while (++index < nodes.length) {
result = one(h, nodes[index], parent)

if (result) {
Expand Down
2 changes: 1 addition & 1 deletion lib/handlers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = base

function base(h, node) {
if (!h.baseFound) {
h.frozenBaseUrl = node.properties.href || null
h.frozenBaseUrl = node.properties.href
h.baseFound = true
}
}
27 changes: 11 additions & 16 deletions lib/handlers/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,38 @@

module.exports = code

var is = require('hast-util-is-element')
var has = require('hast-util-has-property')
var convert = require('hast-util-is-element/convert')
var toText = require('hast-util-to-text')
var trim = require('trim-trailing-lines')
var wrapText = require('../util/wrap-text')

var prefix = 'language-'

var pre = convert('pre')
var isCode = convert('code')

function code(h, node) {
var children = node.children
var length = children.length
var index = -1
var child
var classList
var className
var lang

if (node.tagName === 'pre') {
while (++index < length) {
child = children[index]

if (is(child, 'code') && has(child, 'className')) {
classList = child.properties.className
if (pre(node)) {
while (++index < children.length) {
if (isCode(children[index]) && has(children[index], 'className')) {
classList = children[index].properties.className
break
}
}
}

if (classList) {
length = classList.length
index = -1

while (++index < length) {
className = classList[index]

if (className.slice(0, prefix.length) === prefix) {
lang = className.slice(prefix.length)
while (++index < classList.length) {
if (classList[index].slice(0, prefix.length) === prefix) {
lang = classList[index].slice(prefix.length)
break
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/handlers/delete.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

module.exports = strikethrough
module.exports = del

var all = require('../all')

function strikethrough(h, node) {
function del(h, node) {
return h(node, 'delete', all(h, node))
}
60 changes: 31 additions & 29 deletions lib/handlers/dl.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
'use strict'

module.exports = dataList
module.exports = dl

var is = require('hast-util-is-element')
var wrapListItems = require('../util/wrap-list-items')
var convert = require('hast-util-is-element/convert')
var spread = require('../util/list-items-spread')
var wrapListItems = require('../util/wrap-list-items')

function dataList(h, node) {
var div = convert('div')
var dt = convert('dt')
var dd = convert('dd')

function dl(h, node) {
var children = node.children
var length = children.length
var index = -1
var clean = []
var groups = []
var group = {titles: [], definitions: []}
var content
var breakpoint
var title
var child
var group = {titles: [], definitions: []}

// Unwrap `<div>`s
while (++index < length) {
while (++index < children.length) {
child = children[index]
clean = clean.concat(is(child, 'div') ? child.children : child)
clean = clean.concat(div(child) ? child.children : child)
}

length = clean.length
index = -1

// Group titles and definitions.
while (++index < length) {
while (++index < clean.length) {
child = clean[index]
title = is(child, 'dt')

if (title && breakpoint) {
groups.push(group)
group = {titles: [], definitions: []}
}
if (dt(child)) {
if (dd(clean[index - 1])) {
groups.push(group)
group = {titles: [], definitions: []}
}

group[title ? 'titles' : 'definitions'].push(child)
breakpoint = is(child, 'dd')
group.titles.push(child)
} else {
group.definitions.push(child)
}
}

groups.push(group)

// Create items.
length = groups.length
index = -1
content = []

while (++index < length) {
group = groups[index]
group = handle(h, group.titles).concat(handle(h, group.definitions))
while (++index < groups.length) {
group = handle(h, groups[index].titles).concat(
handle(h, groups[index].definitions)
)

if (group.length > 0) {
if (group.length) {
content.push({
type: 'listItem',
spread: group.length > 1,
Expand All @@ -63,7 +65,7 @@ function dataList(h, node) {
}

// Create a list if there are items.
if (content.length > 0) {
if (content.length) {
return h(
node,
'list',
Expand All @@ -73,11 +75,11 @@ function dataList(h, node) {
}
}

function handle(h, category) {
var nodes = wrapListItems(h, {children: category})
function handle(h, children) {
var nodes = wrapListItems(h, {children: children})

if (nodes.length === 0) {
return []
if (!nodes.length) {
return nodes
}

if (nodes.length === 1) {
Expand Down
8 changes: 3 additions & 5 deletions lib/handlers/heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ module.exports = heading
var all = require('../all')

function heading(h, node) {
var rank = Number(node.tagName.charAt(1))
/* istanbul ignore next - `else` shouldn’t happen, of course… */
var depth = Number(node.tagName.charAt(1)) || 1
var wrap = h.wrapText
var result

/* istanbul ignore next - `else` shouldn’t happen, of course… */
rank = rank || 1

h.wrapText = false
result = h(node, 'heading', {depth: rank}, all(h, node))
result = h(node, 'heading', {depth: depth}, all(h, node))
h.wrapText = wrap

return result
Expand Down
6 changes: 2 additions & 4 deletions lib/handlers/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ module.exports = image
var resolve = require('../util/resolve')

function image(h, node) {
var props = {
return h(node, 'image', {
url: resolve(h, node.properties.src),
title: node.properties.title || null,
alt: node.properties.alt || ''
}

return h(node, 'image', props)
})
}
8 changes: 4 additions & 4 deletions lib/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ var wrapped = require('../util/wrap-children')
var base = require('./base')
var blockquote = require('./blockquote')
var br = require('./break')
var cell = require('./table-cell')
var code = require('./code')
var comment = require('./comment')
var dl = require('./dl')
var del = require('./delete')
var dl = require('./dl')
var emphasis = require('./emphasis')
var heading = require('./heading')
var iframe = require('./iframe')
var image = require('./image')
var inlineCode = require('./inline-code')
var input = require('./input')
var link = require('./link')
var list = require('./list')
var listItem = require('./list-item')
var list = require('./list')
var media = require('./media')
var paragraph = require('./paragraph')
var quote = require('./q')
var root = require('./root')
var row = require('./table-row')
var select = require('./select')
var strong = require('./strong')
var cell = require('./table-cell')
var row = require('./table-row')
var table = require('./table')
var text = require('./text')
var textarea = require('./textarea')
Expand Down
Loading

0 comments on commit 3c18027

Please sign in to comment.