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.7 to 1.4.0

588

dist/mokuji.js
/*!
* mokuji.js v1.3.5
* mokuji.js v1.4.0
* https://github.com/hiro0218/mokuji.js
*
* Copyright (C) 2017 hiro
* Copyright (C) 2017-2018 hiro
*/

@@ -16,3 +16,3 @@ (function webpackUniversalModuleDefinition(root, factory) {

root["Mokuji"] = factory();
})(this, function() {
})(typeof self !== 'undefined' ? self : this, function() {
return /******/ (function(modules) { // webpackBootstrap

@@ -85,38 +85,24 @@ /******/ // The module cache

/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
// CONCATENATED MODULE: ./src/hasParentNode.js
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.init = undefined;
/**
* hasParentNode
* @param {DOM} element
* @param {DOM} parent
* @return {Boolean}
*/
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; }; }();
var _hasParentNode = __webpack_require__(1);
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; }; }();
var _hasParentNode2 = _interopRequireDefault(_hasParentNode);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
__webpack_require__(1).polyfill();
__webpack_require__(2).polyfill();
var defaults = {

@@ -127,4 +113,3 @@ anchorType: true,

anchorLinkBefore: true,
anchorLinkClassName: '',
smoothScroll: true
anchorLinkClassName: ''
};

@@ -134,3 +119,3 @@

var src_init = function () {
var init = exports.init = function () {
function init(element, options) {

@@ -144,3 +129,3 @@ _classCallCheck(this, init);

// set options
options = Object.assign(defaults, options);
options = _extends(defaults, options);

@@ -162,6 +147,2 @@ // mokuji start

if (!mokuji) {
return;
}
// setup anchor link

@@ -172,7 +153,2 @@ if (options.anchorLink) {

// setup smooth scroll
if (options.smoothScroll) {
this.setSmoothScroll(mokuji);
}
return mokuji;

@@ -205,3 +181,3 @@ }

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

@@ -315,19 +291,2 @@ }

}, {
key: 'setSmoothScroll',
value: function setSmoothScroll(mokuji) {
if (!mokuji) {
return;
}
var lists = mokuji.getElementsByTagName('a');
for (var i = 0; i < lists.length; i++) {
lists[i].addEventListener('click', function (e) {
var hash = this.hash;
e.preventDefault();
document.querySelector('[id="' + hash.replace('#', '') + '"]').scrollIntoView({ behavior: 'smooth' });
history.pushState(null, null, hash);
});
}
}
}, {
key: 'replaceSpace2Underscore',

@@ -433,506 +392,27 @@ value: function replaceSpace2Underscore(text) {

"use strict";
/**
* Code refactored from Mozilla Developer Network:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
* hasParentNode
* @param {DOM} element
* @param {DOM} parent
* @return {Boolean}
*/
function assign(target, firstSource) {
if (target === undefined || target === null) {
throw new TypeError('Cannot convert first argument to object');
}
var to = Object(target);
for (var i = 1; i < arguments.length; i++) {
var nextSource = arguments[i];
if (nextSource === undefined || nextSource === null) {
continue;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = hasParentNode;
function hasParentNode(element, parent) {
while (element) {
if (element === parent) {
return true;
}
var keysArray = Object.keys(Object(nextSource));
for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
var nextKey = keysArray[nextIndex];
var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
if (desc !== undefined && desc.enumerable) {
to[nextKey] = nextSource[nextKey];
}
}
element = element.parentNode;
}
return to;
return false;
}
function polyfill() {
if (!Object.assign) {
Object.defineProperty(Object, 'assign', {
enumerable: false,
configurable: true,
writable: true,
value: assign
});
}
}
module.exports = {
assign: assign,
polyfill: polyfill
};
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
/* smoothscroll v0.4.0 - 2017 - Dustan Kasten, Jeremias Menichelli - MIT License */
(function () {
'use strict';
/*
* aliases
* w: window global object
* d: document
*/
var w = window;
var d = document;
/**
* 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 if scroll behavior is supported and polyfill is not forced
if ('scrollBehavior' in d.documentElement.style
&& w.__forceSmoothScrollPolyfill__ !== true) {
return;
}
// globals
var Element = w.HTMLElement || w.Element;
var SCROLL_TIME = 468;
/*
* 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,
elementScroll: Element.prototype.scroll || scrollElement,
scrollIntoView: Element.prototype.scrollIntoView
};
// define timing method
var now = w.performance && w.performance.now
? w.performance.now.bind(w.performance)
: Date.now;
/**
* changes scroll position inside an element
* @method scrollElement
* @param {Number} x
* @param {Number} y
* @returns {undefined}
*/
function scrollElement(x, y) {
this.scrollLeft = x;
this.scrollTop = y;
}
/**
* returns result of applying ease math function to a number
* @method ease
* @param {Number} k
* @returns {Number}
*/
function ease(k) {
return 0.5 * (1 - Math.cos(Math.PI * k));
}
/**
* indicates if a smooth behavior should be applied
* @method shouldBailOut
* @param {Number|Object} firstArg
* @returns {Boolean}
*/
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
return true;
}
if (typeof firstArg === 'object' && firstArg.behavior === 'smooth') {
// first argument is an object and behavior is smooth
return false;
}
// throw error when behavior is not supported
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
* @method findScrollableParent
* @param {Node} el
* @returns {Node} el
*/
function findScrollableParent(el) {
var isBody;
do {
el = el.parentNode;
isBody = el === d.body;
} while (isBody === false && isScrollable(el) === false);
isBody = null;
return el;
}
/**
* self invoked function that, given a context, steps through scrolling
* @method step
* @param {Object} context
* @returns {undefined}
*/
function step(context) {
var time = now();
var value;
var currentX;
var currentY;
var elapsed = (time - context.startTime) / SCROLL_TIME;
// avoid elapsed times higher than one
elapsed = elapsed > 1 ? 1 : elapsed;
// apply easing to elapsed time
value = ease(elapsed);
currentX = context.startX + (context.x - context.startX) * value;
currentY = context.startY + (context.y - context.startY) * value;
context.method.call(context.scrollable, currentX, currentY);
// scroll more if we have not reached our destination
if (currentX !== context.x || currentY !== context.y) {
w.requestAnimationFrame(step.bind(w, context));
}
}
/**
* scrolls window or element with a smooth behavior
* @method smoothScroll
* @param {Object|Node} el
* @param {Number} x
* @param {Number} y
* @returns {undefined}
*/
function smoothScroll(el, x, y) {
var scrollable;
var startX;
var startY;
var method;
var startTime = now();
// define scroll context
if (el === d.body) {
scrollable = w;
startX = w.scrollX || w.pageXOffset;
startY = w.scrollY || w.pageYOffset;
method = original.scroll;
} else {
scrollable = el;
startX = el.scrollLeft;
startY = el.scrollTop;
method = scrollElement;
}
// scroll looping over a frame
step({
scrollable: scrollable,
method: method,
startTime: startTime,
startX: startX,
startY: startY,
x: x,
y: y
});
}
// 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]) === true) {
original.scroll.call(
w,
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;
}
// LET THE SMOOTHNESS BEGIN!
smoothScroll.call(
w,
d.body,
arguments[0].left !== undefined
? ~~arguments[0].left
: (w.scrollX || w.pageXOffset),
arguments[0].top !== undefined
? ~~arguments[0].top
: (w.scrollY || w.pageYOffset)
);
};
// w.scrollBy
w.scrollBy = function() {
// avoid action when no arguments are passed
if (arguments[0] === undefined) {
return;
}
// avoid smooth behavior if not required
if (shouldBailOut(arguments[0])) {
original.scrollBy.call(
w,
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;
}
// LET THE SMOOTHNESS BEGIN!
smoothScroll.call(
w,
d.body,
~~arguments[0].left + (w.scrollX || w.pageXOffset),
~~arguments[0].top + (w.scrollY || w.pageYOffset)
);
};
// Element.prototype.scroll and Element.prototype.scrollTo
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]) === 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,
typeof left === 'undefined' ? this.scrollLeft : ~~left,
typeof top === 'undefined' ? this.scrollTop : ~~top
);
};
// Element.prototype.scrollBy
Element.prototype.scrollBy = function() {
// avoid action when no arguments are passed
if (arguments[0] === undefined) {
return;
}
// 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
});
};
// Element.prototype.scrollIntoView
Element.prototype.scrollIntoView = function() {
// avoid smooth behavior if not required
if (shouldBailOut(arguments[0]) === true) {
original.scrollIntoView.call(
this,
arguments[0] === undefined
? true
: arguments[0]
);
return;
}
// LET THE SMOOTHNESS BEGIN!
var scrollableParent = findScrollableParent(this);
var parentRects = scrollableParent.getBoundingClientRect();
var clientRects = this.getBoundingClientRect();
if (scrollableParent !== d.body) {
// reveal element inside parent
smoothScroll.call(
this,
scrollableParent,
scrollableParent.scrollLeft + clientRects.left - parentRects.left,
scrollableParent.scrollTop + clientRects.top - parentRects.top
);
// reveal parent in viewport unless is fixed
if (w.getComputedStyle(scrollableParent).position !== 'fixed') {
w.scrollBy({
left: parentRects.left,
top: parentRects.top,
behavior: 'smooth'
});
}
} else {
// reveal element in viewport
w.scrollBy({
left: clientRects.left,
top: clientRects.top,
behavior: 'smooth'
});
}
};
}
if (true) {
// commonjs
module.exports = { polyfill: polyfill };
} else {
// global
polyfill();
}
}());
/***/ })
/******/ ]);
});

6

dist/mokuji.min.js
/*!
* mokuji.js v1.3.5
* mokuji.js v1.4.0
* https://github.com/hiro0218/mokuji.js
*
* Copyright (C) 2017 hiro
* Copyright (C) 2017-2018 hiro
*/
!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);var o=this.render(t,r);return a=null,o}}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("#","")+'"]');if(i){var 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}}()}])});
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.Mokuji=r():e.Mokuji=r()}("undefined"!=typeof self?self:this,function(){return function(e){function r(t){if(n[t])return n[t].exports;var o=n[t]={i:t,l:!1,exports:{}};return e[t].call(o.exports,o,o.exports,r),o.l=!0,o.exports}var n={};return r.m=e,r.c=n,r.d=function(e,n,t){r.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:t})},r.n=function(e){var n=e&&e.__esModule?function(){return e["default"]}:function(){return e};return r.d(n,"a",n),n},r.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},r.p="",r(r.s=0)}([function(e,r,n){"use strict";function t(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(r,"__esModule",{value:!0}),r.init=undefined;var o=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var n=arguments[r];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(e[t]=n[t])}return e},a=function(){function e(e,r){for(var n=0;n<r.length;n++){var t=r[n];t.enumerable=t.enumerable||!1,t.configurable=!0,"value"in t&&(t.writable=!0),Object.defineProperty(e,t.key,t)}}return function(r,n,t){return n&&e(r.prototype,n),t&&e(r,t),r}}(),i=n(1),u=function(e){return e&&e.__esModule?e:{default:e}}(i),l={anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:""},c=[];r.init=function(){function e(r,n){if(t(this,e),r){n=o(l,n);var a=this.render(r,n);return c=null,a}}return a(e,[{key:"render",value:function(e,r){var n=this.generateMokuji(e,r);return r.anchorLink&&this.renderAnchorLink(n,r),n}},{key:"generateMokuji",value:function(e,r){for(var n=this.createHeadingWalker(e),t=null,o=0,a=document.createElement("ol"),i=document.createElement("li"),l=document.createElement("a");t=n.nextNode();){var c=t.tagName.match(/\d/g).join("");if(c=Number(c),0!==o&&o<c){var f=document.createElement("ol");a.lastChild.appendChild(f),a=f}else if(0!==o&&o>c)for(var d=0;d<o-c;d++)(0,u["default"])(a,a.parentNode)&&(a=a.parentNode.parentNode);var s=this.censorshipId(t.textContent);t.id=this.setAnchor(t.id,s,r.anchorType),a.appendChild(this.buildList(t,l.cloneNode(!1),i.cloneNode(!1))),o=c}return 0===o?null:(a=this.reverseMokuji(a),this.removeDuplicateIds(a),a)}},{key:"censorshipId",value:function(e){var r=e,n=1;if(-1!==c.indexOf(r))for(;n<10;){var t=r+"_"+n;if(-1===c.indexOf(t)){r=t,c.push(r);break}n++}else c.push(r);return r}},{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,r,n){var t=e||this.replaceSpace2Underscore(r);return t=t.replace(/\&+/g,""),t=t.replace(/\&amp;+/g,""),!0===n&&(t=encodeURIComponent(t),t=t.replace(/\%+/g,".")),t}},{key:"renderAnchorLink",value:function(e,r){if(e){var n=e.getElementsByTagName("a"),t=document.createElement("a");t.classList.add(r.anchorLinkClassName);for(var o=0;o<n.length;o++){var a=n[o].hash,i=document.querySelector('[id="'+a.replace("#","")+'"]');if(i){var u=t.cloneNode(!1);u.setAttribute("href",a),u.textContent=r.anchorLinkSymbol,r.anchorLinkBefore?i.insertBefore(u,i.firstChild):i.appendChild(u)}}}}},{key:"replaceSpace2Underscore",value:function(e){return String(e).replace(/\s+/g,"_")}},{key:"buildList",value:function(e,r,n){return r.href="#"+e.id,r.textContent=e.textContent,n.appendChild(r),n}},{key:"reverseMokuji",value:function(e){for(;e.parentNode;)e=e.parentNode;return e}},{key:"removeDuplicateIds",value:function(e){for(var r=e.getElementsByTagName("a"),n=0;n<r.length;n++){var t=r[n].innerText,o=r[n].hash,a=document.querySelectorAll('[id="'+t+'"]');if(1!==a.length){var i=0;Array.from||(Array.from=function(e){return Array.prototype.slice.call(e)});for(var u=Array.from(a),l=Array.isArray(u),c=0,u=l?u:u[Symbol.iterator]();;){var f;if(l){if(c>=u.length)break;f=u[c++]}else{if(c=u.next(),c.done)break;f=c.value}for(var d=f,s=d.id+"-"+i,p=Array.from(r),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===o){m.href="#"+s;break}}d.id=s,i++}}}}}]),e}()},function(e,r,n){"use strict";function t(e,r){for(;e;){if(e===r)return!0;e=e.parentNode}return!1}Object.defineProperty(r,"__esModule",{value:!0}),r["default"]=t}])});

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

export default function hasParentNode(element, parent) {
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = hasParentNode;
function hasParentNode(element, parent) {
while (element) {

@@ -19,2 +23,2 @@ if (element === parent) {

return false;
};
}
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.init = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; }; }();
var _hasParentNode = require('./hasParentNode');
var _hasParentNode2 = _interopRequireDefault(_hasParentNode);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
require('es6-object-assign').polyfill();
require('smoothscroll-polyfill').polyfill();
import hasParentNode from './hasParentNode';
var defaults = {

@@ -16,4 +25,3 @@ anchorType: true,

anchorLinkBefore: true,
anchorLinkClassName: '',
smoothScroll: true
anchorLinkClassName: ''
};

@@ -23,3 +31,3 @@

export var init = function () {
var init = exports.init = function () {
function init(element, options) {

@@ -33,3 +41,3 @@ _classCallCheck(this, init);

// set options
options = Object.assign(defaults, options);
options = _extends(defaults, options);

@@ -51,6 +59,2 @@ // mokuji start

if (!mokuji) {
return;
}
// setup anchor link

@@ -61,7 +65,2 @@ if (options.anchorLink) {

// setup smooth scroll
if (options.smoothScroll) {
this.setSmoothScroll(mokuji);
}
return mokuji;

@@ -94,3 +93,3 @@ }

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

@@ -204,19 +203,2 @@ }

}, {
key: 'setSmoothScroll',
value: function setSmoothScroll(mokuji) {
if (!mokuji) {
return;
}
var lists = mokuji.getElementsByTagName('a');
for (var i = 0; i < lists.length; i++) {
lists[i].addEventListener('click', function (e) {
var hash = this.hash;
e.preventDefault();
document.querySelector('[id="' + hash.replace('#', '') + '"]').scrollIntoView({ behavior: 'smooth' });
history.pushState(null, null, hash);
});
}
}
}, {
key: 'replaceSpace2Underscore',

@@ -223,0 +205,0 @@ value: function replaceSpace2Underscore(text) {

{
"name": "mokuji.js",
"version": "1.3.7",
"version": "1.4.0",
"description": "A table of content JavaScript Library",

@@ -12,4 +12,13 @@ "main": "lib/index.js",

"lint": "eslint src",
"format": "eslint --fix src",
"precommit": "lint-staged",
"test": "npm run lint"
},
"lint-staged": {
"src/*.js": [
"eslint --fix",
"eslint",
"git add"
]
},
"repository": {

@@ -31,18 +40,24 @@ "type": "git",

"homepage": "https://github.com/hiro0218/mokuji.js",
"dependencies": {
"es6-object-assign": "^1.1.0",
"smoothscroll-polyfill": "^0.4.0"
},
"dependencies": {},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.2",
"babel-eslint": "^8.1.2",
"babel-loader": "^7.1.2",
"babel-plugin-transform-es2015-for-of": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"cross-env": "^5.1.1",
"eslint": "^4.11.0",
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-preset-env": "^1.6.1",
"cross-env": "^5.1.3",
"eslint": "^4.14.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-prettier": "^2.4.0",
"eslint-plugin-promise": "^3.6.0",
"husky": "^0.14.3",
"lint-staged": "^6.0.0",
"prettier": "^1.9.2",
"rimraf": "^2.6.2",
"webpack": "^3.8.1"
"webpack": "^3.10.0"
}
}

@@ -35,3 +35,2 @@ mokuji.js

anchorLinkClassName: '',
smoothScroll: true
}

@@ -74,7 +73,1 @@ ```

### `smoothScroll`
(default: `true`)
enable/disable native smooth scrolling.

@@ -7,3 +7,3 @@ var webpack = require('webpack');

Copyright (C) 2017 ${package.author}`;
Copyright (C) 2017-2018 ${package.author}`;

@@ -39,2 +39,3 @@ var config = {

}),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.AggressiveMergingPlugin(),

@@ -41,0 +42,0 @@ );

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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