editor: Count lines for numbered list

This commit is contained in:
Eragon 2023-06-06 21:00:59 +02:00
parent 57644c4378
commit 7aa93f15ea
Signed by untrusted user: Eragon
GPG Key ID: 087126EBFC725006
1 changed files with 6 additions and 6 deletions

View File

@ -94,7 +94,7 @@ function editor_act_on_lines(event, fn)
let lines = ta.value.substring(firstLineIndex, lastLineIndex).split('\n');
for(let i = 0; i < lines.length; i++)
lines[i] = fn(lines[i]);
lines[i] = fn(lines[i], i);
let [start, end] = editor_replace_range(ta, firstLineIndex, lastLineIndex,
lines.join('\n'));
@ -202,7 +202,7 @@ function editor_insert_link(event, link_id, text_id, media = false)
function editor_title(event, level, diff)
{
editor_act_on_lines(event, function(line) {
editor_act_on_lines(event, function(line, _) {
/* Strip all the initial # (and count them) */
let count = 0;
while(count < line.length && line[count] == '#') count++;
@ -230,7 +230,7 @@ function editor_title(event, level, diff)
function editor_quote(event)
{
editor_act_on_lines(event, function(line) {
editor_act_on_lines(event, function(line, _) {
/* Strip all the initial > (and count them) */
let count = 0;
while(count < line.length && line[count] == '>') count++;
@ -246,7 +246,7 @@ function editor_quote(event)
function editor_bullet_list(event)
{
editor_act_on_lines(event, function(line) {
editor_act_on_lines(event, function(line, _) {
let ident_match = line.match(/^[\t]+/m) ?? [''];
let ident = ident_match[0];
let count = ident.length;
@ -260,13 +260,13 @@ function editor_bullet_list(event)
function editor_numbered_list(event)
{
editor_act_on_lines(event, function(line) {
editor_act_on_lines(event, function(line, number) {
let ident_match = line.match(/^[\t]+/m) ?? [''];
let ident = ident_match[0];
let count = ident.length;
const contents = line.slice(count);
if ((count < line.length || count == 0) && isNaN(line[count])) return '1. ' + contents;
if ((count < line.length || count == 0) && isNaN(line[count])) return `${number + 1}. ` + contents;
return ident + "\t" + contents;
});