@sanity/block-tools
Advanced tools
Comparing version 0.123.0 to 0.124.0-alpha.35b4b49a
@@ -122,2 +122,4 @@ 'use strict'; | ||
// Trim whitespace from the DOM that is between HTML-elements and | ||
// become appended to block spans in the wrong places | ||
function trimWhitespace(blocks) { | ||
@@ -155,2 +157,5 @@ blocks.forEach(function (block) { | ||
}); | ||
if (block.children.length === 0) { | ||
block.children.push({ _type: 'span', marks: [], text: '' }); | ||
} | ||
}); | ||
@@ -157,0 +162,0 @@ return blocks; |
@@ -68,2 +68,3 @@ 'use strict'; | ||
}; | ||
this.blocks = []; | ||
} | ||
@@ -137,2 +138,5 @@ | ||
nodes.push(node); | ||
if (node._type === 'block') { | ||
_this.blocks.push(node); | ||
} | ||
break; | ||
@@ -178,3 +182,3 @@ default: | ||
} | ||
var ret = rule.deserialize(element, next); | ||
var ret = rule.deserialize(element, next, _this.blocks, _this.deserializeElements); | ||
var type = (0, _resolveJsType2.default)(ret); | ||
@@ -255,3 +259,3 @@ | ||
} | ||
} else { | ||
} else if (node.children) { | ||
node.children = node.children.map(applyDecorator); | ||
@@ -284,3 +288,3 @@ } | ||
} | ||
} else { | ||
} else if (node.children) { | ||
node.children = node.children.map(applyAnnotation); | ||
@@ -287,0 +291,0 @@ } |
@@ -20,3 +20,5 @@ "use strict"; | ||
// xPaths for elements that will be removed from the document | ||
var unwantedPaths = ["//*[name() = 'o:p']"]; | ||
var unwantedPaths = ["//*[name() = 'o:p']" | ||
// "//span[@style='mso-list:Ignore']", | ||
]; | ||
@@ -53,4 +55,4 @@ // xPaths for elements that needs to be remapped into other tags | ||
var tags = elementMap[mappedElm.className]; | ||
var text = new Text(mappedElm.textContent); | ||
var parentElement = document.createElement(tags[0]); | ||
var text = doc.createTextNode(mappedElm.textContent); | ||
var parentElement = doc.createElement(tags[0]); | ||
var parent = parentElement; | ||
@@ -57,0 +59,0 @@ var child = parentElement; |
@@ -11,2 +11,6 @@ 'use strict'; | ||
var _randomKey = require('../../util/randomKey'); | ||
var _randomKey2 = _interopRequireDefault(_randomKey); | ||
var _constants = require('../../constants'); | ||
@@ -16,11 +20,20 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// https://gist.github.com/webtobesocial/ac9d052595b406d5a5c1 | ||
function notesEnabled(options) { | ||
return options.enabledBlockAnnotations.includes('blockNote'); | ||
} | ||
function isNormalEmptyParagraph(el) { | ||
return (0, _helpers.tagName)(el) === 'p' && el.textContent === '' && el.textContent === '' && el.className === 'MsoNormal'; | ||
} | ||
function getListItemStyle(el) { | ||
var style = void 0; | ||
if (style = el.getAttribute('style')) { | ||
if (!style.match(/lfo\d+/)) { | ||
return undefined; | ||
} | ||
return style.match('lfo1') ? 'bullet' : 'number'; | ||
var symbol = el.textContent.trim(); | ||
if (symbol.match(/\b\./)) { | ||
return 'number'; | ||
} | ||
return undefined; | ||
return 'bullet'; | ||
} | ||
@@ -41,3 +54,3 @@ | ||
function isWordListElement(el) { | ||
function isListElement(el) { | ||
if (el.className) { | ||
@@ -49,2 +62,34 @@ return el.className === 'MsoListParagraphCxSpFirst' || el.className === 'MsoListParagraphCxSpMiddle' || el.className === 'MsoListParagraphCxSpLast'; | ||
function getFootnoteContentElementId(el) { | ||
var style = el.getAttribute('style'); | ||
if (style && style === 'mso-element:footnote') { | ||
return el.getAttribute('id').trim(); | ||
} | ||
return null; | ||
} | ||
function getFootnoteLinkElementId(el) { | ||
var style = el.getAttribute('style'); | ||
if (style && style.match(/mso-footnote-id/)) { | ||
return style.split(':')[1].trim(); | ||
} | ||
return null; | ||
} | ||
function getEndnoteContentElementId(el) { | ||
var style = el.getAttribute('style'); | ||
if (style && style === 'mso-element:endnote') { | ||
return el.getAttribute('id').trim(); | ||
} | ||
return null; | ||
} | ||
function getEndnoteLinkElementId(el) { | ||
var style = el.getAttribute('style'); | ||
if (style && style.match(/mso-endnote-id/)) { | ||
return style.split(':')[1].trim(); | ||
} | ||
return null; | ||
} | ||
function createWordRules(blockContentType) { | ||
@@ -54,7 +99,29 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return [{ | ||
return [ | ||
// Fix weird paragraphing within Word (paragraph is more of a line break) | ||
// If we see two empty paragraphs after each other, we return an empty block | ||
{ | ||
deserialize: function deserialize(el, next) { | ||
if ((0, _helpers.tagName)(el) === 'p' && isWordListElement(el)) { | ||
if (isNormalEmptyParagraph(el)) { | ||
var nextSibling = el.nextElementSibling; | ||
if (nextSibling && isNormalEmptyParagraph(nextSibling)) { | ||
return _extends({}, _constants.DEFAULT_BLOCK, { | ||
style: 'normal', | ||
children: [{ _type: 'span', marks: [], text: '' }] | ||
}); | ||
} | ||
return next([]); | ||
} | ||
return undefined; | ||
} | ||
}, | ||
// List elements | ||
{ | ||
deserialize: function deserialize(el, next) { | ||
if ((0, _helpers.tagName)(el) === 'p' && isListElement(el)) { | ||
var listItem = el.querySelector("span[style='mso-list:Ignore']"); | ||
var listItemStyle = getListItemStyle(listItem); | ||
listItem.parentNode.removeChild(listItem); | ||
return _extends({}, _constants.DEFAULT_BLOCK, { | ||
listItem: getListItemStyle(el), | ||
listItem: listItemStyle, | ||
level: getListItemLevel(el), | ||
@@ -67,3 +134,101 @@ style: 'normal', | ||
} | ||
}, | ||
// Fotnote links | ||
{ | ||
deserialize: function deserialize(el, next) { | ||
var footnoteId = void 0; | ||
if ((0, _helpers.tagName)(el) === 'a' && (footnoteId = getFootnoteLinkElementId(el))) { | ||
if (!notesEnabled(options)) { | ||
return undefined; | ||
} | ||
var markDef = { | ||
_key: (0, _randomKey2.default)(12), | ||
_type: 'blockNote', | ||
style: 'footnote', | ||
blockNoteId: footnoteId | ||
}; | ||
return { | ||
_type: '__annotation', | ||
markDef: markDef, | ||
children: next(el.childNodes) | ||
}; | ||
} | ||
return undefined; | ||
} | ||
}, | ||
// Footnote content | ||
{ | ||
deserialize: function deserialize(el, next, blocks, _deserialize) { | ||
var footnoteId = void 0; | ||
if ((0, _helpers.tagName)(el) === 'div' && (footnoteId = getFootnoteContentElementId(el))) { | ||
if (!notesEnabled(options)) { | ||
return undefined; | ||
} | ||
// Find the block where the footnote occured | ||
var markDef = blocks.map(function (blk) { | ||
return blk.markDefs.find(function (def) { | ||
return def.blockNoteId === footnoteId; | ||
}); | ||
}).filter(Boolean)[0]; | ||
if (markDef) { | ||
el.querySelectorAll('a[name=\'_' + footnoteId + '\']').forEach(function (elm) { | ||
elm.parentNode.removeChild(elm); | ||
}); | ||
markDef.content = _deserialize(el.childNodes); | ||
delete markDef.blockNoteId; | ||
} | ||
return next([]); | ||
} | ||
return undefined; | ||
} | ||
}, | ||
// Endnote links | ||
{ | ||
deserialize: function deserialize(el, next) { | ||
var endnoteId = void 0; | ||
if ((0, _helpers.tagName)(el) === 'a' && (endnoteId = getEndnoteLinkElementId(el))) { | ||
if (!notesEnabled(options)) { | ||
return undefined; | ||
} | ||
var markDef = { | ||
_key: (0, _randomKey2.default)(12), | ||
_type: 'blockNote', | ||
style: 'endnote', | ||
blockNoteId: endnoteId | ||
}; | ||
return { | ||
_type: '__annotation', | ||
markDef: markDef, | ||
children: next(el.childNodes) | ||
}; | ||
} | ||
return undefined; | ||
} | ||
}, | ||
// Endnote content | ||
{ | ||
deserialize: function deserialize(el, next, blocks, _deserialize2) { | ||
var endnoteId = void 0; | ||
if ((0, _helpers.tagName)(el) === 'div' && (endnoteId = getEndnoteContentElementId(el))) { | ||
if (!notesEnabled(options)) { | ||
return undefined; | ||
} | ||
// Find the block where the footnote occured | ||
var markDef = blocks.map(function (blk) { | ||
return blk.markDefs.find(function (def) { | ||
return def.blockNoteId === endnoteId; | ||
}); | ||
}).filter(Boolean)[0]; | ||
if (markDef) { | ||
el.querySelectorAll('a[name=\'_' + endnoteId + '\']').forEach(function (elm) { | ||
elm.parentNode.removeChild(elm); | ||
}); | ||
markDef.content = _deserialize2(el.childNodes); | ||
delete markDef.blockNoteId; | ||
} | ||
return next([]); | ||
} | ||
return undefined; | ||
} | ||
}]; | ||
} |
{ | ||
"name": "@sanity/block-tools", | ||
"version": "0.123.0", | ||
"version": "0.124.0-alpha.35b4b49a", | ||
"description": "Can format HTML, Slate JSON or Sanity block array into any other format.", | ||
@@ -39,3 +39,3 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@sanity/schema": "^0.113.7", | ||
"@sanity/schema": "0.124.0-alpha.35b4b49a", | ||
"assert": "^1.4.1", | ||
@@ -68,2 +68,3 @@ "babel-cli": "^6.24.1", | ||
"jsdom": "11.3.0", | ||
"wgxpath": "^1.2.0", | ||
"xpath": "^0.0.24" | ||
@@ -70,0 +71,0 @@ }, |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
226751
24
1456
30
1