Skip to content

Commit

Permalink
keep compat with markdown-it-anchor 5.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
nagaozen committed Jun 19, 2018
1 parent 501e59a commit 4f839c1
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 1,370 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [2.0.3] - 2018-06-19
### Changed
- update to keep compat with `markdown-it-anchor` `v5.0.2`

## [2.0.2] - 2018-06-15
### Changed
- tests cover 100%
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ build:
upddemo:
rm -rf ./lib
mkdir lib
curl -o lib/markdown-it.js https://wzrd.in/standalone/markdown-it@latest
curl -o lib/markdown-it-anchor.js https://wzrd.in/standalone/markdown-it-anchor@latest
curl -o lib/uslug.js https://wzrd.in/standalone/uslug@latest
7 changes: 7 additions & 0 deletions __test__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ test("headers innerText may happen more than once", () => {
`);
});

test("and sometimes slugify with suffix may generate another existing header", () => {
expect( md.render("${toc}\n\n# header\n\n## header\n\n## header 2") ).toBe(`<nav class="table-of-contents"><ol><li><a href="#header"> header</a><ol><li><a href="#header-2"> header</a></li><li><a href="#header-2-2"> header 2</a></li></ol></li></ol></nav><h1 id="header"><a class="header-anchor" href="#header" aria-hidden="true">§</a> header</h1>
<h2 id="header-2"><a class="header-anchor" href="#header-2" aria-hidden="true">§</a> header</h2>
<h2 id="header-2-2"><a class="header-anchor" href="#header-2-2" aria-hidden="true">§</a> header 2</h2>
`);
});

test("all options should work as expected", () => {
expect( umd.render("@[[TOC]]\n\n# 日本語") ).toBe('<nav class="user-content-toc"><ul><li><a href="#日本語"><span> 日本語</span></a></li></ul></nav><h1 id="日本語"><a class="header-anchor" href="#日本語" aria-hidden="true">§</a> 日本語</h1>\n');
});
2 changes: 1 addition & 1 deletion demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

</head>
<body>
<script src="lib/markdown-it.js"></script>
<script src="https://unpkg.com/markdown-it@8.4.1/dist/markdown-it.min.js"></script>
<script src="lib/markdown-it-anchor.js"></script>
<script src="lib/uslug.js"></script>
<script src="dist/markdown-it-toc-made-right.min.js"></script>
Expand Down
37 changes: 20 additions & 17 deletions dist/markdown-it-toc-made-right.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! markdown-it-toc-done-right 2.0.0 https://github.com//nagaozen/markdown-it-toc-done-right @license MIT */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.markdownitTocDoneRight = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
/*! markdown-it-toc-done-right 2.0.2 https://github.com//nagaozen/markdown-it-toc-done-right @license MIT */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.markdownitTocDoneRight = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";

function slugify(x) {
Expand Down Expand Up @@ -28,7 +28,7 @@ module.exports = function toc_plugin(md, options) {

var final_state = void 0;

function toc(state, startLine /*, endLine, silent*/) {
function toc(state, startLine, endLine, silent) {
var token = void 0;
var pos = state.bMarks[startLine] + state.tShift[startLine];
var max = state.eMarks[startLine];
Expand All @@ -41,21 +41,21 @@ module.exports = function toc_plugin(md, options) {
if (state.src.charCodeAt(pos + i) !== options.placeholder.charCodeAt(i) || pos >= max) return false;
}

/*if(silent) return true;*/
if (!silent) {
state.line = startLine + 1;

state.line = startLine + 1;
token = state.push("toc_open", "nav", 1);
token.markup = options.placeholder;
token.map = [startLine, state.line];

token = state.push("toc_open", "nav", 1);
token.markup = options.placeholder;
token.map = [startLine, state.line];
token = state.push("toc_body", "", 0);
token.markup = options.placeholder;
token.map = [startLine, state.line];
token.children = [];

token = state.push("toc_body", "", 0);
token.markup = options.placeholder;
token.map = [startLine, state.line];
token.children = [];

token = state.push("toc_close", "nav", -1);
token.markup = options.placeholder;
token = state.push("toc_close", "nav", -1);
token.markup = options.placeholder;
}

return true;
}
Expand All @@ -75,9 +75,12 @@ module.exports = function toc_plugin(md, options) {
function ast_html(tree, uniques) {
uniques = uniques || {};
function unique(s) {
if (uniques.hasOwnProperty(s)) return s + "-" + ++uniques[s];
uniques[s] = 1;
return s;
var u = s;
var i = 2;
while (uniques.hasOwnProperty(u)) {
u = s + "-" + i++;
}uniques[u] = true;
return u;
}

var keys = Object.keys(tree.c);
Expand Down
2 changes: 1 addition & 1 deletion dist/markdown-it-toc-made-right.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = function toc_plugin(md, options) {
if (state.src.charCodeAt(pos + i) !== options.placeholder.charCodeAt(i) || pos >= max) return false;
}

/*if(silent) return true;*/
// if(silent) return true;

state.line = startLine + 1;

Expand Down Expand Up @@ -78,9 +78,11 @@ module.exports = function toc_plugin(md, options) {
function ast_html(tree, uniques) {
uniques = uniques || {};
function unique(s) {
if( uniques.hasOwnProperty(s) ) return `${s}-${++uniques[s]}`;
uniques[s] = 1;
return s;
let u = s;
let i = 2;
while (uniques.hasOwnProperty(u)) u = `${s}-${i++}`;
uniques[u] = true;
return u;
}

const keys = Object.keys(tree.c);
Expand Down
Loading

0 comments on commit 4f839c1

Please sign in to comment.