Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mokuji.js

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mokuji.js - npm Package Compare versions

Comparing version 1.3.4 to 1.3.5

375

dist/mokuji.js

@@ -88,7 +88,26 @@ /*!

Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "init", function() { return init; });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__hasParentNode__ = __webpack_require__(3);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__hasParentNode___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__hasParentNode__);
// CONCATENATED MODULE: ./src/hasParentNode.js
/**
* hasParentNode
* @param {DOM} element
* @param {DOM} parent
* @return {Boolean}
*/
function hasParentNode(element, parent) {
while (element) {
if (element === parent) {
return true;
}
element = element.parentNode;
}
return false;
};
// CONCATENATED MODULE: ./src/index.js
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "init", function() { return src_init; });
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

@@ -103,3 +122,3 @@

var defaults = {
anchorType: '',
anchorType: true,
anchorLink: false,

@@ -112,3 +131,5 @@ anchorLinkSymbol: '#',

var init = function () {
var storeIds = [];
var src_init = function () {
function init(element, options) {

@@ -177,3 +198,3 @@ _classCallCheck(this, init);

for (var i = 0; i < number - currentNumber; i++) {
if (__WEBPACK_IMPORTED_MODULE_0__hasParentNode___default()(ol, ol.parentNode)) {
if (hasParentNode(ol, ol.parentNode)) {
ol = ol.parentNode.parentNode;

@@ -184,4 +205,6 @@ }

var textContent = this.censorshipId(node.textContent);
// add to wrapper
node.id = this.setAnchor(node.id, node.textContent, options.anchorType);
node.id = this.setAnchor(node.id, textContent, options.anchorType);
ol.appendChild(this.buildList(node, a.cloneNode(false), li.cloneNode(false)));

@@ -206,2 +229,24 @@

}, {
key: 'censorshipId',
value: function censorshipId(textContent) {
var id = textContent;
var count = 1;
if (storeIds.indexOf(id) !== -1) {
while (count < 10) {
var tmp_id = id + '_' + count;
if (storeIds.indexOf(tmp_id) === -1) {
id = tmp_id;
storeIds.push(id);
break;
}
count++;
}
} else {
storeIds.push(id);
}
return id;
}
}, {
key: 'createHeadingWalker',

@@ -224,3 +269,3 @@ value: function createHeadingWalker(element) {

if (type === 'wikipedia') {
if (type === true) {
anchor = encodeURIComponent(anchor);

@@ -432,9 +477,4 @@ anchor = anchor.replace(/\%+/g, '.');

/*
* smoothscroll polyfill - v0.3.5
* https://iamdustan.github.io/smoothscroll
* 2016 (c) Dustan Kasten, Jeremias Menichelli - MIT License
*/
(function(w, d, undefined) {
/* smoothscroll v0.4.0 - 2017 - Dustan Kasten, Jeremias Menichelli - MIT License */
(function () {
'use strict';

@@ -446,15 +486,27 @@

* d: document
* undefined: undefined
*/
var w = window;
var d = document;
// polyfill
/**
* indicates if a the current browser is made by Microsoft
* @method isMicrosoftBrowser
* @param {String} userAgent
* @returns {Boolean}
*/
function isMicrosoftBrowser(userAgent) {
var userAgentPatterns = ['MSIE ', 'Trident/', 'Edge/'];
return new RegExp(userAgentPatterns.join('|')).test(userAgent);
}
// polyfill
function polyfill() {
// return when scrollBehavior interface is supported
if ('scrollBehavior' in d.documentElement.style) {
// return if scroll behavior is supported and polyfill is not forced
if ('scrollBehavior' in d.documentElement.style
&& w.__forceSmoothScrollPolyfill__ !== true) {
return;
}
/*
* globals
*/
// globals
var Element = w.HTMLElement || w.Element;

@@ -464,16 +516,20 @@ var SCROLL_TIME = 468;

/*
* object gathering original scroll methods
* IE has rounding bug rounding down clientHeight and clientWidth and
* rounding up scrollHeight and scrollWidth causing false positives
* on hasScrollableSpace
*/
var ROUNDING_TOLERANCE = isMicrosoftBrowser(w.navigator.userAgent) ? 1 : 0;
// object gathering original scroll methods
var original = {
scroll: w.scroll || w.scrollTo,
scrollBy: w.scrollBy,
elScroll: Element.prototype.scroll || scrollElement,
elementScroll: Element.prototype.scroll || scrollElement,
scrollIntoView: Element.prototype.scrollIntoView
};
/*
* define timing method
*/
// define timing method
var now = w.performance && w.performance.now
? w.performance.now.bind(w.performance) : Date.now;
? w.performance.now.bind(w.performance)
: Date.now;

@@ -485,2 +541,3 @@ /**

* @param {Number} y
* @returns {undefined}
*/

@@ -505,12 +562,12 @@ function scrollElement(x, y) {

* @method shouldBailOut
* @param {Number|Object} x
* @param {Number|Object} firstArg
* @returns {Boolean}
*/
function shouldBailOut(x) {
if (typeof x !== 'object'
|| x === null
|| x.behavior === undefined
|| x.behavior === 'auto'
|| x.behavior === 'instant') {
// first arg not an object/null
function shouldBailOut(firstArg) {
if (firstArg === null
|| typeof firstArg !== 'object'
|| firstArg.behavior === undefined
|| firstArg.behavior === 'auto'
|| firstArg.behavior === 'instant') {
// first argument is not an object/null
// or behavior is auto, instant or undefined

@@ -520,4 +577,3 @@ return true;

if (typeof x === 'object'
&& x.behavior === 'smooth') {
if (typeof firstArg === 'object' && firstArg.behavior === 'smooth') {
// first argument is an object and behavior is smooth

@@ -528,6 +584,54 @@ return false;

// throw error when behavior is not supported
throw new TypeError('behavior not valid');
throw new TypeError(
'behavior member of ScrollOptions '
+ firstArg.behavior
+ ' is not a valid value for enumeration ScrollBehavior.'
);
}
/**
* indicates if an element has scrollable space in the provided axis
* @method hasScrollableSpace
* @param {Node} el
* @param {String} axis
* @returns {Boolean}
*/
function hasScrollableSpace(el, axis) {
if (axis === 'Y') {
return (el.clientHeight + ROUNDING_TOLERANCE) < el.scrollHeight;
}
if (axis === 'X') {
return (el.clientWidth + ROUNDING_TOLERANCE) < el.scrollWidth;
}
}
/**
* indicates if an element has a scrollable overflow property in the axis
* @method canOverflow
* @param {Node} el
* @param {String} axis
* @returns {Boolean}
*/
function canOverflow(el, axis) {
var overflowValue = w.getComputedStyle(el, null)['overflow' + axis];
return overflowValue === 'auto' || overflowValue === 'scroll';
}
/**
* indicates if an element can be scrolled in either axis
* @method isScrollable
* @param {Node} el
* @param {String} axis
* @returns {Boolean}
*/
function isScrollable(el) {
var isScrollableY = hasScrollableSpace(el, 'Y') && canOverflow(el, 'Y');
var isScrollableX = hasScrollableSpace(el, 'X') && canOverflow(el, 'X');
return isScrollableY || isScrollableX;
}
/**
* finds scrollable parent of an element

@@ -540,4 +644,2 @@ * @method findScrollableParent

var isBody;
var hasScrollableSpace;
var hasVisibleOverflow;

@@ -547,12 +649,6 @@ do {

// set condition variables
isBody = el === d.body;
hasScrollableSpace =
el.clientHeight < el.scrollHeight ||
el.clientWidth < el.scrollWidth;
hasVisibleOverflow =
w.getComputedStyle(el, null).overflow === 'visible';
} while (!isBody && !(hasScrollableSpace && !hasVisibleOverflow));
} while (isBody === false && isScrollable(el) === false);
isBody = hasScrollableSpace = hasVisibleOverflow = null;
isBody = null;

@@ -566,2 +662,3 @@ return el;

* @param {Object} context
* @returns {undefined}
*/

@@ -593,3 +690,3 @@ function step(context) {

/**
* scrolls window with a smooth behavior
* scrolls window or element with a smooth behavior
* @method smoothScroll

@@ -599,2 +696,3 @@ * @param {Object|Node} el

* @param {Number} y
* @returns {undefined}
*/

@@ -633,15 +731,27 @@ function smoothScroll(el, x, y) {

/*
* ORIGINAL METHODS OVERRIDES
*/
// ORIGINAL METHODS OVERRIDES
// w.scroll and w.scrollTo
w.scroll = w.scrollTo = function() {
// avoid action when no arguments are passed
if (arguments[0] === undefined) {
return;
}
// avoid smooth behavior if not required
if (shouldBailOut(arguments[0])) {
if (shouldBailOut(arguments[0]) === true) {
original.scroll.call(
w,
arguments[0].left || arguments[0],
arguments[0].top || arguments[1]
arguments[0].left !== undefined
? arguments[0].left
: typeof arguments[0] !== 'object'
? arguments[0]
: (w.scrollX || w.pageXOffset),
// use top prop, second argument if present or fallback to scrollY
arguments[0].top !== undefined
? arguments[0].top
: arguments[1] !== undefined
? arguments[1]
: (w.scrollY || w.pageYOffset)
);
return;

@@ -654,4 +764,8 @@ }

d.body,
~~arguments[0].left,
~~arguments[0].top
arguments[0].left !== undefined
? ~~arguments[0].left
: (w.scrollX || w.pageXOffset),
arguments[0].top !== undefined
? ~~arguments[0].top
: (w.scrollY || w.pageYOffset)
);

@@ -662,2 +776,7 @@ };

w.scrollBy = function() {
// avoid action when no arguments are passed
if (arguments[0] === undefined) {
return;
}
// avoid smooth behavior if not required

@@ -667,5 +786,14 @@ if (shouldBailOut(arguments[0])) {

w,
arguments[0].left || arguments[0],
arguments[0].top || arguments[1]
arguments[0].left !== undefined
? arguments[0].left
: typeof arguments[0] !== 'object'
? arguments[0]
: 0,
arguments[0].top !== undefined
? arguments[0].top
: arguments[1] !== undefined
? arguments[1]
: 0
);
return;

@@ -685,18 +813,42 @@ }

Element.prototype.scroll = Element.prototype.scrollTo = function() {
// avoid action when no arguments are passed
if (arguments[0] === undefined) {
return;
}
// avoid smooth behavior if not required
if (shouldBailOut(arguments[0])) {
original.elScroll.call(
this,
arguments[0].left || arguments[0],
arguments[0].top || arguments[1]
if (shouldBailOut(arguments[0]) === true) {
// if one number is passed, throw error to match Firefox implementation
if (typeof arguments[0] === 'number' && arguments[1] === undefined) {
throw new SyntaxError('Value couldn\'t be converted');
}
original.elementScroll.call(
this,
// use left prop, first number argument or fallback to scrollLeft
arguments[0].left !== undefined
? ~~arguments[0].left
: typeof arguments[0] !== 'object'
? ~~arguments[0]
: this.scrollLeft,
// use top prop, second argument or fallback to scrollTop
arguments[0].top !== undefined
? ~~arguments[0].top
: arguments[1] !== undefined
? ~~arguments[1]
: this.scrollTop
);
return;
}
var left = arguments[0].left;
var top = arguments[0].top;
// LET THE SMOOTHNESS BEGIN!
smoothScroll.call(
this,
this,
arguments[0].left,
arguments[0].top
this,
this,
typeof left === 'undefined' ? this.scrollLeft : ~~left,
typeof top === 'undefined' ? this.scrollTop : ~~top
);

@@ -707,16 +859,27 @@ };

Element.prototype.scrollBy = function() {
var arg0 = arguments[0];
// avoid action when no arguments are passed
if (arguments[0] === undefined) {
return;
}
if (typeof arg0 === 'object') {
this.scroll({
left: arg0.left + this.scrollLeft,
top: arg0.top + this.scrollTop,
behavior: arg0.behavior
});
} else {
this.scroll(
this.scrollLeft + arg0,
this.scrollTop + arguments[1]
// avoid smooth behavior if not required
if (shouldBailOut(arguments[0]) === true) {
original.elementScroll.call(
this,
arguments[0].left !== undefined
? ~~arguments[0].left + this.scrollLeft
: ~~arguments[0] + this.scrollLeft,
arguments[0].top !== undefined
? ~~arguments[0].top + this.scrollTop
: ~~arguments[1] + this.scrollTop
);
return;
}
this.scroll({
left: ~~arguments[0].left + this.scrollLeft,
top: ~~arguments[0].top + this.scrollTop,
behavior: arguments[0].behavior
});
};

@@ -727,4 +890,10 @@

// avoid smooth behavior if not required
if (shouldBailOut(arguments[0])) {
original.scrollIntoView.call(this, arguments[0] || true);
if (shouldBailOut(arguments[0]) === true) {
original.scrollIntoView.call(
this,
arguments[0] === undefined
? true
: arguments[0]
);
return;

@@ -746,8 +915,11 @@ }

);
// reveal parent in viewport
w.scrollBy({
left: parentRects.left,
top: parentRects.top,
behavior: 'smooth'
});
// reveal parent in viewport unless is fixed
if (w.getComputedStyle(scrollableParent).position !== 'fixed') {
w.scrollBy({
left: parentRects.left,
top: parentRects.top,
behavior: 'smooth'
});
}
} else {

@@ -771,31 +943,8 @@ // reveal element in viewport

}
})(window, document);
}());
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/**
* hasParentNode
* @param {DOM} element
* @param {DOM} parent
* @return {Boolean}
*/
module.exports = function hasParentNode(element, parent) {
while (element) {
if (element === parent) {
return true;
}
element = element.parentNode;
}
return false;
};
/***/ })
/******/ ]);
});

@@ -7,2 +7,2 @@ /*!

*/
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Mokuji=t():e.Mokuji=t()}(this,function(){return function(e){function t(o){if(r[o])return r[o].exports;var n=r[o]={i:o,l:!1,exports:{}};return e[o].call(n.exports,n,n.exports,t),n.l=!0,n.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,o){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:o})},t.n=function(e){var r=e&&e.__esModule?function(){return e["default"]}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,r){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0}),r.d(t,"init",function(){return c});var n=r(3),l=r.n(n),i=function(){function e(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,r,o){return r&&e(t.prototype,r),o&&e(t,o),t}}();r(1).polyfill(),r(2).polyfill();var a={anchorType:"",anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",smoothScroll:!0},c=function(){function e(t,r){if(o(this,e),t){r=Object.assign(a,r);return this.render(t,r)}}return i(e,[{key:"render",value:function(e,t){var r=this.generateMokuji(e,t);if(r)return t.anchorLink&&this.renderAnchorLink(r,t),t.smoothScroll&&this.setSmoothScroll(r),r}},{key:"generateMokuji",value:function(e,t){for(var r=this.createHeadingWalker(e),o=null,n=0,i=document.createElement("ol"),a=document.createElement("li"),c=document.createElement("a");o=r.nextNode();){var s=o.tagName.match(/\d/g).join("");if(s=Number(s),0!==n&&n<s){var u=document.createElement("ol");i.lastChild.appendChild(u),i=u}else if(0!==n&&n>s)for(var f=0;f<n-s;f++)l()(i,i.parentNode)&&(i=i.parentNode.parentNode);o.id=this.setAnchor(o.id,o.textContent,t.anchorType),i.appendChild(this.buildList(o,c.cloneNode(!1),a.cloneNode(!1))),n=s}return 0===n?null:(i=this.reverseMokuji(i),this.removeDuplicateIds(i),i)}},{key:"createHeadingWalker",value:function(e){return document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT,function(e){return/^H[1-6]$/.test(e.tagName)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP},!1)}},{key:"setAnchor",value:function(e,t,r){var o=e||this.replaceSpace2Underscore(t);return o=o.replace(/\&+/g,""),o=o.replace(/\&amp;+/g,""),"wikipedia"===r&&(o=encodeURIComponent(o),o=o.replace(/\%+/g,".")),o}},{key:"renderAnchorLink",value:function(e,t){if(e){var r=e.getElementsByTagName("a"),o=document.createElement("a");o.classList.add(t.anchorLinkClassName);for(var n=0;n<r.length;n++){var l=r[n].hash,i=document.querySelector('[id="'+l.replace("#","")+'"]'),a=o.cloneNode(!1);a.setAttribute("href",l),a.textContent=t.anchorLinkSymbol,t.anchorLinkBefore?i.insertBefore(a,i.firstChild):i.appendChild(a)}}}},{key:"setSmoothScroll",value:function(e){if(e)for(var t=e.getElementsByTagName("a"),r=0;r<t.length;r++)t[r].addEventListener("click",function(e){var t=this.hash;e.preventDefault(),document.querySelector('[id="'+t.replace("#","")+'"]').scrollIntoView({behavior:"smooth"}),history.pushState(null,null,t)})}},{key:"replaceSpace2Underscore",value:function(e){return String(e).replace(/\s+/g,"_")}},{key:"buildList",value:function(e,t,r){return t.href="#"+e.id,t.textContent=e.textContent,r.appendChild(t),r}},{key:"reverseMokuji",value:function(e){for(;e.parentNode;)e=e.parentNode;return e}},{key:"removeDuplicateIds",value:function(e){for(var t=e.getElementsByTagName("a"),r=0;r<t.length;r++){var o=t[r].innerText,n=t[r].hash,l=document.querySelectorAll('[id="'+o+'"]');if(1!==l.length){var i=0;Array.from||(Array.from=function(e){return Array.prototype.slice.call(e)});for(var a=Array.from(l),c=Array.isArray(a),s=0,a=c?a:a[Symbol.iterator]();;){var u;if(c){if(s>=a.length)break;u=a[s++]}else{if(s=a.next(),s.done)break;u=s.value}for(var f=u,p=f.id+"-"+i,d=Array.from(t),h=Array.isArray(d),y=0,d=h?d:d[Symbol.iterator]();;){var v;if(h){if(y>=d.length)break;v=d[y++]}else{if(y=d.next(),y.done)break;v=y.value}var m=v;if(m.hash===n){m.href="#"+p;break}}f.id=p,i++}}}}}]),e}()},function(e,t,r){"use strict";function o(e,t){if(e===undefined||null===e)throw new TypeError("Cannot convert first argument to object");for(var r=Object(e),o=1;o<arguments.length;o++){var n=arguments[o];if(n!==undefined&&null!==n)for(var l=Object.keys(Object(n)),i=0,a=l.length;i<a;i++){var c=l[i],s=Object.getOwnPropertyDescriptor(n,c);s!==undefined&&s.enumerable&&(r[c]=n[c])}}return r}function n(){Object.assign||Object.defineProperty(Object,"assign",{enumerable:!1,configurable:!0,writable:!0,value:o})}e.exports={assign:o,polyfill:n}},function(e,t,r){!function(t,r,o){"use strict";function n(){function e(e,t){this.scrollLeft=e,this.scrollTop=t}function n(e){return.5*(1-Math.cos(Math.PI*e))}function l(e){if("object"!=typeof e||null===e||e.behavior===o||"auto"===e.behavior||"instant"===e.behavior)return!0;if("object"==typeof e&&"smooth"===e.behavior)return!1;throw new TypeError("behavior not valid")}function i(e){var o,n,l;do{e=e.parentNode,o=e===r.body,n=e.clientHeight<e.scrollHeight||e.clientWidth<e.scrollWidth,l="visible"===t.getComputedStyle(e,null).overflow}while(!o&&(!n||l));return o=n=l=null,e}function a(e){var r,o,l,i=p(),c=(i-e.startTime)/u;c=c>1?1:c,r=n(c),o=e.startX+(e.x-e.startX)*r,l=e.startY+(e.y-e.startY)*r,e.method.call(e.scrollable,o,l),o===e.x&&l===e.y||t.requestAnimationFrame(a.bind(t,e))}function c(o,n,l){var i,c,s,u,d=p();o===r.body?(i=t,c=t.scrollX||t.pageXOffset,s=t.scrollY||t.pageYOffset,u=f.scroll):(i=o,c=o.scrollLeft,s=o.scrollTop,u=e),a({scrollable:i,method:u,startTime:d,startX:c,startY:s,x:n,y:l})}if(!("scrollBehavior"in r.documentElement.style)){var s=t.HTMLElement||t.Element,u=468,f={scroll:t.scroll||t.scrollTo,scrollBy:t.scrollBy,elScroll:s.prototype.scroll||e,scrollIntoView:s.prototype.scrollIntoView},p=t.performance&&t.performance.now?t.performance.now.bind(t.performance):Date.now;t.scroll=t.scrollTo=function(){if(l(arguments[0]))return void f.scroll.call(t,arguments[0].left||arguments[0],arguments[0].top||arguments[1]);c.call(t,r.body,~~arguments[0].left,~~arguments[0].top)},t.scrollBy=function(){if(l(arguments[0]))return void f.scrollBy.call(t,arguments[0].left||arguments[0],arguments[0].top||arguments[1]);c.call(t,r.body,~~arguments[0].left+(t.scrollX||t.pageXOffset),~~arguments[0].top+(t.scrollY||t.pageYOffset))},s.prototype.scroll=s.prototype.scrollTo=function(){if(l(arguments[0]))return void f.elScroll.call(this,arguments[0].left||arguments[0],arguments[0].top||arguments[1]);c.call(this,this,arguments[0].left,arguments[0].top)},s.prototype.scrollBy=function(){var e=arguments[0];"object"==typeof e?this.scroll({left:e.left+this.scrollLeft,top:e.top+this.scrollTop,behavior:e.behavior}):this.scroll(this.scrollLeft+e,this.scrollTop+arguments[1])},s.prototype.scrollIntoView=function(){if(l(arguments[0]))return void f.scrollIntoView.call(this,arguments[0]||!0);var e=i(this),o=e.getBoundingClientRect(),n=this.getBoundingClientRect();e!==r.body?(c.call(this,e,e.scrollLeft+n.left-o.left,e.scrollTop+n.top-o.top),t.scrollBy({left:o.left,top:o.top,behavior:"smooth"})):t.scrollBy({left:n.left,top:n.top,behavior:"smooth"})}}}e.exports={polyfill:n}}(window,document)},function(e,t,r){"use strict";e.exports=function(e,t){for(;e;){if(e===t)return!0;e=e.parentNode}return!1}}])});
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Mokuji=t():e.Mokuji=t()}(this,function(){return function(e){function t(o){if(r[o])return r[o].exports;var n=r[o]={i:o,l:!1,exports:{}};return e[o].call(n.exports,n,n.exports,t),n.l=!0,n.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,o){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:o})},t.n=function(e){var r=e&&e.__esModule?function(){return e["default"]}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,r){"use strict";function o(e,t){for(;e;){if(e===t)return!0;e=e.parentNode}return!1}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0}),r.d(t,"init",function(){return c});var l=function(){function e(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,r,o){return r&&e(t.prototype,r),o&&e(t,o),t}}();r(1).polyfill(),r(2).polyfill();var i={anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",smoothScroll:!0},a=[],c=function(){function e(t,r){if(n(this,e),t){r=Object.assign(i,r);return this.render(t,r)}}return l(e,[{key:"render",value:function(e,t){var r=this.generateMokuji(e,t);if(r)return t.anchorLink&&this.renderAnchorLink(r,t),t.smoothScroll&&this.setSmoothScroll(r),r}},{key:"generateMokuji",value:function(e,t){for(var r=this.createHeadingWalker(e),n=null,l=0,i=document.createElement("ol"),a=document.createElement("li"),c=document.createElement("a");n=r.nextNode();){var f=n.tagName.match(/\d/g).join("");if(f=Number(f),0!==l&&l<f){var s=document.createElement("ol");i.lastChild.appendChild(s),i=s}else if(0!==l&&l>f)for(var u=0;u<l-f;u++)o(i,i.parentNode)&&(i=i.parentNode.parentNode);var d=this.censorshipId(n.textContent);n.id=this.setAnchor(n.id,d,t.anchorType),i.appendChild(this.buildList(n,c.cloneNode(!1),a.cloneNode(!1))),l=f}return 0===l?null:(i=this.reverseMokuji(i),this.removeDuplicateIds(i),i)}},{key:"censorshipId",value:function(e){var t=e,r=1;if(-1!==a.indexOf(t))for(;r<10;){var o=t+"_"+r;if(-1===a.indexOf(o)){t=o,a.push(t);break}r++}else a.push(t);return t}},{key:"createHeadingWalker",value:function(e){return document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT,function(e){return/^H[1-6]$/.test(e.tagName)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP},!1)}},{key:"setAnchor",value:function(e,t,r){var o=e||this.replaceSpace2Underscore(t);return o=o.replace(/\&+/g,""),o=o.replace(/\&amp;+/g,""),!0===r&&(o=encodeURIComponent(o),o=o.replace(/\%+/g,".")),o}},{key:"renderAnchorLink",value:function(e,t){if(e){var r=e.getElementsByTagName("a"),o=document.createElement("a");o.classList.add(t.anchorLinkClassName);for(var n=0;n<r.length;n++){var l=r[n].hash,i=document.querySelector('[id="'+l.replace("#","")+'"]'),a=o.cloneNode(!1);a.setAttribute("href",l),a.textContent=t.anchorLinkSymbol,t.anchorLinkBefore?i.insertBefore(a,i.firstChild):i.appendChild(a)}}}},{key:"setSmoothScroll",value:function(e){if(e)for(var t=e.getElementsByTagName("a"),r=0;r<t.length;r++)t[r].addEventListener("click",function(e){var t=this.hash;e.preventDefault(),document.querySelector('[id="'+t.replace("#","")+'"]').scrollIntoView({behavior:"smooth"}),history.pushState(null,null,t)})}},{key:"replaceSpace2Underscore",value:function(e){return String(e).replace(/\s+/g,"_")}},{key:"buildList",value:function(e,t,r){return t.href="#"+e.id,t.textContent=e.textContent,r.appendChild(t),r}},{key:"reverseMokuji",value:function(e){for(;e.parentNode;)e=e.parentNode;return e}},{key:"removeDuplicateIds",value:function(e){for(var t=e.getElementsByTagName("a"),r=0;r<t.length;r++){var o=t[r].innerText,n=t[r].hash,l=document.querySelectorAll('[id="'+o+'"]');if(1!==l.length){var i=0;Array.from||(Array.from=function(e){return Array.prototype.slice.call(e)});for(var a=Array.from(l),c=Array.isArray(a),f=0,a=c?a:a[Symbol.iterator]();;){var s;if(c){if(f>=a.length)break;s=a[f++]}else{if(f=a.next(),f.done)break;s=f.value}for(var u=s,d=u.id+"-"+i,p=Array.from(t),h=Array.isArray(p),v=0,p=h?p:p[Symbol.iterator]();;){var y;if(h){if(v>=p.length)break;y=p[v++]}else{if(v=p.next(),v.done)break;y=v.value}var m=y;if(m.hash===n){m.href="#"+d;break}}u.id=d,i++}}}}}]),e}()},function(e,t,r){"use strict";function o(e,t){if(e===undefined||null===e)throw new TypeError("Cannot convert first argument to object");for(var r=Object(e),o=1;o<arguments.length;o++){var n=arguments[o];if(n!==undefined&&null!==n)for(var l=Object.keys(Object(n)),i=0,a=l.length;i<a;i++){var c=l[i],f=Object.getOwnPropertyDescriptor(n,c);f!==undefined&&f.enumerable&&(r[c]=n[c])}}return r}function n(){Object.assign||Object.defineProperty(Object,"assign",{enumerable:!1,configurable:!0,writable:!0,value:o})}e.exports={assign:o,polyfill:n}},function(e,t,r){!function(){"use strict";function t(e){var t=["MSIE ","Trident/","Edge/"];return new RegExp(t.join("|")).test(e)}function r(){function e(e,t){this.scrollLeft=e,this.scrollTop=t}function r(e){return.5*(1-Math.cos(Math.PI*e))}function l(e){if(null===e||"object"!=typeof e||e.behavior===undefined||"auto"===e.behavior||"instant"===e.behavior)return!0;if("object"==typeof e&&"smooth"===e.behavior)return!1;throw new TypeError("behavior member of ScrollOptions "+e.behavior+" is not a valid value for enumeration ScrollBehavior.")}function i(e,t){return"Y"===t?e.clientHeight+h<e.scrollHeight:"X"===t?e.clientWidth+h<e.scrollWidth:void 0}function a(e,t){var r=o.getComputedStyle(e,null)["overflow"+t];return"auto"===r||"scroll"===r}function c(e){var t=i(e,"Y")&&a(e,"Y"),r=i(e,"X")&&a(e,"X");return t||r}function f(e){var t;do{e=e.parentNode,t=e===n.body}while(!1===t&&!1===c(e));return t=null,e}function s(e){var t,n,l,i=y(),a=(i-e.startTime)/p;a=a>1?1:a,t=r(a),n=e.startX+(e.x-e.startX)*t,l=e.startY+(e.y-e.startY)*t,e.method.call(e.scrollable,n,l),n===e.x&&l===e.y||o.requestAnimationFrame(s.bind(o,e))}function u(t,r,l){var i,a,c,f,u=y();t===n.body?(i=o,a=o.scrollX||o.pageXOffset,c=o.scrollY||o.pageYOffset,f=v.scroll):(i=t,a=t.scrollLeft,c=t.scrollTop,f=e),s({scrollable:i,method:f,startTime:u,startX:a,startY:c,x:r,y:l})}if(!("scrollBehavior"in n.documentElement.style&&!0!==o.__forceSmoothScrollPolyfill__)){var d=o.HTMLElement||o.Element,p=468,h=t(o.navigator.userAgent)?1:0,v={scroll:o.scroll||o.scrollTo,scrollBy:o.scrollBy,elementScroll:d.prototype.scroll||e,scrollIntoView:d.prototype.scrollIntoView},y=o.performance&&o.performance.now?o.performance.now.bind(o.performance):Date.now;o.scroll=o.scrollTo=function(){if(arguments[0]!==undefined)return!0===l(arguments[0])?void v.scroll.call(o,arguments[0].left!==undefined?arguments[0].left:"object"!=typeof arguments[0]?arguments[0]:o.scrollX||o.pageXOffset,arguments[0].top!==undefined?arguments[0].top:arguments[1]!==undefined?arguments[1]:o.scrollY||o.pageYOffset):void u.call(o,n.body,arguments[0].left!==undefined?~~arguments[0].left:o.scrollX||o.pageXOffset,arguments[0].top!==undefined?~~arguments[0].top:o.scrollY||o.pageYOffset)},o.scrollBy=function(){if(arguments[0]!==undefined)return l(arguments[0])?void v.scrollBy.call(o,arguments[0].left!==undefined?arguments[0].left:"object"!=typeof arguments[0]?arguments[0]:0,arguments[0].top!==undefined?arguments[0].top:arguments[1]!==undefined?arguments[1]:0):void u.call(o,n.body,~~arguments[0].left+(o.scrollX||o.pageXOffset),~~arguments[0].top+(o.scrollY||o.pageYOffset))},d.prototype.scroll=d.prototype.scrollTo=function(){if(arguments[0]!==undefined){if(!0===l(arguments[0])){if("number"==typeof arguments[0]&&arguments[1]===undefined)throw new SyntaxError("Value couldn't be converted");return void v.elementScroll.call(this,arguments[0].left!==undefined?~~arguments[0].left:"object"!=typeof arguments[0]?~~arguments[0]:this.scrollLeft,arguments[0].top!==undefined?~~arguments[0].top:arguments[1]!==undefined?~~arguments[1]:this.scrollTop)}var e=arguments[0].left,t=arguments[0].top;u.call(this,this,void 0===e?this.scrollLeft:~~e,void 0===t?this.scrollTop:~~t)}},d.prototype.scrollBy=function(){if(arguments[0]!==undefined)return!0===l(arguments[0])?void v.elementScroll.call(this,arguments[0].left!==undefined?~~arguments[0].left+this.scrollLeft:~~arguments[0]+this.scrollLeft,arguments[0].top!==undefined?~~arguments[0].top+this.scrollTop:~~arguments[1]+this.scrollTop):void this.scroll({left:~~arguments[0].left+this.scrollLeft,top:~~arguments[0].top+this.scrollTop,behavior:arguments[0].behavior})},d.prototype.scrollIntoView=function(){if(!0===l(arguments[0]))return void v.scrollIntoView.call(this,arguments[0]===undefined||arguments[0]);var e=f(this),t=e.getBoundingClientRect(),r=this.getBoundingClientRect();e!==n.body?(u.call(this,e,e.scrollLeft+r.left-t.left,e.scrollTop+r.top-t.top),"fixed"!==o.getComputedStyle(e).position&&o.scrollBy({left:t.left,top:t.top,behavior:"smooth"})):o.scrollBy({left:r.left,top:r.top,behavior:"smooth"})}}}var o=window,n=document;e.exports={polyfill:r}}()}])});

@@ -10,3 +10,3 @@ 'use strict';

module.exports = function hasParentNode(element, parent) {
export default function hasParentNode(element, parent) {
while (element) {

@@ -13,0 +13,0 @@ if (element === parent) {

@@ -12,3 +12,3 @@ 'use strict';

var defaults = {
anchorType: '',
anchorType: true,
anchorLink: false,

@@ -21,2 +21,4 @@ anchorLinkSymbol: '#',

var storeIds = [];
export var init = function () {

@@ -92,4 +94,6 @@ function init(element, options) {

var textContent = this.censorshipId(node.textContent);
// add to wrapper
node.id = this.setAnchor(node.id, node.textContent, options.anchorType);
node.id = this.setAnchor(node.id, textContent, options.anchorType);
ol.appendChild(this.buildList(node, a.cloneNode(false), li.cloneNode(false)));

@@ -114,2 +118,24 @@

}, {
key: 'censorshipId',
value: function censorshipId(textContent) {
var id = textContent;
var count = 1;
if (storeIds.indexOf(id) !== -1) {
while (count < 10) {
var tmp_id = id + '_' + count;
if (storeIds.indexOf(tmp_id) === -1) {
id = tmp_id;
storeIds.push(id);
break;
}
count++;
}
} else {
storeIds.push(id);
}
return id;
}
}, {
key: 'createHeadingWalker',

@@ -132,3 +158,3 @@ value: function createHeadingWalker(element) {

if (type === 'wikipedia') {
if (type === true) {
anchor = encodeURIComponent(anchor);

@@ -135,0 +161,0 @@ anchor = anchor.replace(/\%+/g, '.');

{
"name": "mokuji.js",
"version": "1.3.4",
"version": "1.3.5",
"description": "A table of content JavaScript Library",
"main": "lib/index.js",
"scripts": {
"build": "babel src --out-dir lib",
"build": "babel src --out-dir lib && npm run build:umd && npm run build:umd:min",
"build:umd": "webpack src/index.js dist/mokuji.js",

@@ -32,15 +32,16 @@ "build:umd:min": "cross-env NODE_ENV=production webpack src/index.js dist/mokuji.min.js",

"es6-object-assign": "^1.1.0",
"smoothscroll-polyfill": "^0.3.5"
"smoothscroll-polyfill": "^0.4.0"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.3",
"babel-loader": "^7.1.1",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.2",
"babel-loader": "^7.1.2",
"babel-plugin-transform-es2015-for-of": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"cross-env": "^5.0.1",
"eslint": "^4.3.0",
"rimraf": "^2.6.1",
"webpack": "^3.4.1"
"cross-env": "^5.1.1",
"eslint": "^4.11.0",
"rimraf": "^2.6.2",
"webpack": "^3.8.1"
}
}

@@ -30,3 +30,3 @@ mokuji.js

{
anchorType: '',
anchorType: true,
anchorLink: false,

@@ -42,5 +42,5 @@ anchorLinkSymbol: '#',

(default: `''`)
(default: `true`)
`wikipedia`: Like Wikipedia's anchor, multibyte characters are replaced by escape sequences.
Like Wikipedia's anchor, multibyte characters are replaced by escape sequences.

@@ -47,0 +47,0 @@ `こんにちは` → `.E3.81.93.E3.82.93.E3.81.AB.E3.81.A1.E3.81.AF`

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc