feat: allow add id for code block

This commit is contained in:
linonetwo 2023-09-16 02:38:06 +08:00
parent 4c407c28e4
commit 18236b547f
3 changed files with 16 additions and 5 deletions

View File

@ -17,8 +17,10 @@ Instantiate parse rule
*/
exports.init = function(parser) {
this.parser = parser;
// Regexp to match the block identifier located on the end of the line.
this.matchRegExp = /[ ]\^\S+$/mg;
// Regexp to match the block identifier
// 1. located on the end of the line, with a space before it, means it's the id of the current block.
// 2. located at start of the line, no space, means it's the id of the previous block. Because some block can't have id suffix, otherwise id break the block mode parser like codeblock.
this.matchRegExp = /[ ]\^(\S+)$|^\^(\S+)$/mg;
};
/*
@ -27,12 +29,17 @@ Parse the most recent match
exports.parse = function() {
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
var id = this.match[0].slice(2);
// will be one of following case, another will be undefined
var blockId = this.match[1];
var blockBeforeId = this.match[2];
// Parse tree nodes to return
return [{
type: "blockid",
attributes: {
id: {type: "string", value: id}
id: {type: "string", value: blockId || blockBeforeId},
// `true` means the block is before this node, in parent node's children list.
// `false` means the block is this node's parent node.
before: {type: "boolean", value: Boolean(blockBeforeId)},
},
children: []
}];

View File

@ -21,6 +21,9 @@ BlockIdWidget.prototype.render = function(parent,nextSibling) {
// Create an invisible DOM element with data that can be accessed from JS or CSS
this.spanDomNode = this.document.createElement("span");
this.spanDomNode.setAttribute("data-id",this.id);
if(this.before) {
this.spanDomNode.setAttribute("data-before","true");
}
this.spanDomNode.className = "tc-block-id";
parent.insertBefore(this.spanDomNode,nextSibling);
this.domNodes.push(this.spanDomNode);
@ -32,6 +35,7 @@ Compute the internal state of the widget
BlockIdWidget.prototype.execute = function() {
// Get the id from the parse tree node or manually assigned attributes
this.id = this.getAttribute("id");
this.before = this.getAttribute("before");
// Make the child widgets
this.makeChildWidgets();
};

View File

@ -4,7 +4,7 @@ tags: $:/tags/EditTemplate
\whitespace trim
<$edit-text field="draft.title" class="tc-titlebar tc-edit-texteditor" focus={{{ [{$:/config/AutoFocus}match[title]then[true]] ~[[false]] }}} tabindex={{$:/config/EditTabIndex}} cancelPopups="yes"/>
<$vars pattern="""[\|\[\]{}]""" bad-chars="""`| [ ] { }`""">
<$vars pattern="""[\^\|\[\]{}]""" bad-chars="""`| ^ [ ] { }`""">
<$list filter="[all[current]regexp:draft.title<pattern>]" variable="listItem">