Skip to content

Instantly share code, notes, and snippets.

@Quotation
Last active November 24, 2015 05:20
Show Gist options
  • Save Quotation/2fd1ef8a4706ce48db40 to your computer and use it in GitHub Desktop.
Save Quotation/2fd1ef8a4706ce48db40 to your computer and use it in GitHub Desktop.
Tower.im 文档编辑自动编号
javascript:(function() {$('<button type="button" class="btn btn-numbering">标题重新编号</button>') .appendTo($('.save-btns-wrap .form-field')) .click(function() {var numbers = [0, 0, 0, 0, 0, 0]; $('H1,H2,H3,H4,H5,H6', $('.simditor-body')).each(function(i, e) {var level = parseInt(e.tagName[1]); numbers[level - 1] += 1; for (var i = level; i < numbers.length; i++) {numbers[i] = 0; } var prefix = numbers.slice(0, level).join('.'); e = $(e); var title = e.text(); if (!title.startsWith(prefix)) {title = title.replace(/^(\d\.)*\d\s?/, ''); e.text(prefix + ' ' + title); } }); }); })()
(function() {
$('<button type="button" class="btn btn-numbering">标题重新编号</button>')
.appendTo($('.save-btns-wrap .form-field'))
.click(function() {
var numbers = [0, 0, 0, 0, 0, 0];
$('H1,H2,H3,H4,H5,H6', $('.simditor-body')).each(function(i, e) {
var level = parseInt(e.tagName[1]);
numbers[level - 1] += 1;
for (var i = level; i < numbers.length; i++) {
numbers[i] = 0;
}
var prefix = numbers.slice(0, level).join('.');
e = $(e);
var title = e.text();
if (!title.startsWith(prefix)) {
title = title.replace(/^(\d\.)*\d\s?/, '');
e.text(prefix + ' ' + title);
}
});
});
})()
@Quotation
Copy link
Author

为Tower.im的文档自动加上标题编号,效果如下:
#1 一级标题

1.1 二级标题

1.1.1 三级标题


用法

当在Tower编辑文档时,把tower-numbering-bookmark.js的内容拷到浏览器地址栏,回车。会发现编辑器最下方增加了一个按钮“标题重新编号”。每次点击此按钮时,文档内的所有标题即自动编号。

使用限制

  1. 目前只适用于普通文档,不能用于Markdown文档。
  2. 只适用于标题是纯文字的情况,不支持标题里的超链接等等。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment