-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_handler.ts
More file actions
639 lines (588 loc) · 25.8 KB
/
block_handler.ts
File metadata and controls
639 lines (588 loc) · 25.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import {BlockUUID, BlockUUIDTuple} from "@logseq/libs/dist/LSPlugin";
import {BlockEntity} from "@logseq/libs/dist/LSPlugin.user";
import {isEqual} from 'lodash';
class BlockTreeNode {
public refBlock: BlockEntity | undefined;
public children: BlockTreeNode[] = [];
public content: string = "";
public properties: Record<string, any> = {};
public headerLevel = 0;
public blankLevel = 0;
}
class VisitContext {
public parentBlock: BlockEntity | undefined;
public lastVisitedBlock: BlockEntity | undefined;
public visitedBlockUuids: Set<BlockUUID> = new Set();
}
export interface TransformMode {
id: number;
name: string;
useSplit: boolean;
useHeader: boolean;
removeEmptyLine: boolean;
splitCodeBlock: boolean;
orderedToNonOrdered: boolean;
removeTailPunctuation: boolean;
boldToHeader: boolean;
maxHeaderLevel: number;
}
export class TransformerContext {
public useSplit = true;
public useHeader = false;
public removeEmptyLine = true;
public splitCodeBlock = true;
public orderedToNonOrdered = false;
public removeTailPunctuation: boolean = true;
public boldToHeader: boolean = false;
public maxHeaderLevel: number = 4;
}
function camelToKebab(str: string) {
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
function convertBlockProperties(blockProperties: Record<string, any> | undefined): Record<string, any> {
let properties: Record<string, any> = {};
if (blockProperties) {
for (let propertiesKey in blockProperties) {
properties[camelToKebab(propertiesKey)] = blockProperties[propertiesKey];
}
}
return properties
}
async function splitBlocksToTree(blockEntities: BlockEntity[], transformerContext: TransformerContext): Promise<BlockTreeNode[]> {
let outputBlockTreeNodes: BlockTreeNode[] = [];
function appendNewBlockTreeNode(blockTreeNode: BlockTreeNode, is_first: boolean, blockEntity: BlockEntity, lastBlockTreeNodes: BlockTreeNode[]) {
if (is_first) {
is_first = false;
blockTreeNode.refBlock = blockEntity;
// inherent refBlock properties
blockTreeNode.properties = convertBlockProperties(blockEntity.properties);
}
// Find correct parent
for (let j = lastBlockTreeNodes.length - 1; j >= 0; j--) {
const parentCandidate = lastBlockTreeNodes[j];
if (blockTreeNode.headerLevel > 0) { // Current is a header
if (parentCandidate.headerLevel > 0 && parentCandidate.headerLevel >= blockTreeNode.headerLevel) {
lastBlockTreeNodes.pop();
} else if (parentCandidate.headerLevel === 0) { // if parent candidate is not a header, pop it to find a header parent
lastBlockTreeNodes.pop();
} else {
break;
}
} else { // Current is not a header
if (parentCandidate.headerLevel === 0 && parentCandidate.blankLevel >= blockTreeNode.blankLevel) {
lastBlockTreeNodes.pop();
} else if (parentCandidate.headerLevel > 0 && blockTreeNode.headerLevel === 0) {
break;
} else {
break;
}
}
}
if (lastBlockTreeNodes.length == 0) {
// append output
outputBlockTreeNodes.push(blockTreeNode);
lastBlockTreeNodes.push(blockTreeNode);
} else {
// append children
lastBlockTreeNodes[lastBlockTreeNodes.length - 1].children.push(blockTreeNode)
lastBlockTreeNodes.push(blockTreeNode);
}
return is_first;
}
for (let blockEntity of blockEntities) {
let is_first = true;
let lastBlockTreeNodes: BlockTreeNode[] = [];
let lines = getContent(blockEntity).split(/\r\n|\n|\r/);
let completeCodeBlock = true;
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
let blankLevel = 0;
let blankNum = 0;
// find blank len
for (let j = 0; j < line.length; j++) {
if (line[j] === ' ') {
blankNum += 1;
blankLevel += 1;
} else if (line[j] === '\t') {
blankNum += 1;
// one tab equal four spaces
blankLevel += 4;
} else {
break
}
}
// filter empty line
if (blankNum == line.length && transformerContext.removeEmptyLine) {
continue;
}
// filter collapsed property line
if (line.startsWith("collapsed:: ")) {
continue;
}
// handle lines
if (!completeCodeBlock) {
const position = line.indexOf('```')
if (position >= 0) {
lastBlockTreeNodes[lastBlockTreeNodes.length - 1].content += '\n' + line.substring(0, position + 3)
// remove heading blank
let blockLines = lastBlockTreeNodes[lastBlockTreeNodes.length - 1].content.split('\n');
// get the min blank num
let minBlankNum = blockLines.length;
for (let j = 1; j < blockLines.length - 1; j++) {
let blockLine = blockLines[j];
let blankNum = 0;
for (let k = 0; k < blockLine.length; k++) {
if (blockLine[k] === ' ' || blockLine[k] === '\t') {
blankNum += 1;
} else {
break;
}
}
minBlankNum = Math.min(minBlankNum, blankNum);
}
// handle remove heading blank
let content = '';
for (let j = 0; j < blockLines.length; j++) {
if (j == 0) {
content += blockLines[j].trim();
} else if (j == blockLines.length - 1) {
content += '\n' + blockLines[j].trim();
} else {
content += '\n' + blockLines[j].substring(minBlankNum)
}
}
lastBlockTreeNodes[lastBlockTreeNodes.length - 1].content = content;
blankLevel = lastBlockTreeNodes[lastBlockTreeNodes.length - 1].blankLevel
let line1 = line.substring(position + 3);
if (line1.trim().length > 0) {
let blockTreeNode = {
refBlock: undefined,
content: line1,
children: [],
properties: {},
headerLevel: 0,
blankLevel: blankLevel
}
is_first = appendNewBlockTreeNode(blockTreeNode, is_first, blockEntity, lastBlockTreeNodes);
}
completeCodeBlock = true;
} else {
lastBlockTreeNodes[lastBlockTreeNodes.length - 1].content += '\n' + line
}
} else if (line.substring(blankNum, blankNum + 3) === '```') {
completeCodeBlock = false;
if (!transformerContext.splitCodeBlock) {
lastBlockTreeNodes[lastBlockTreeNodes.length - 1].content += '\n' + line
} else {
let blockTreeNode = {
refBlock: undefined,
content: line,
children: [],
properties: {},
headerLevel: 0,
blankLevel: blankLevel
}
is_first = appendNewBlockTreeNode(blockTreeNode, is_first, blockEntity, lastBlockTreeNodes);
}
} else {
// handle header
let match = line.match(/^\s*(#+)\s(.*)$/);
if (match) {
let blockTreeNode = {
refBlock: undefined,
content: match[1] + ' ' + match[2],
children: [],
properties: {heading: match[1].length},
headerLevel: match[1].length,
blankLevel: blankLevel
};
is_first = appendNewBlockTreeNode(blockTreeNode, is_first, blockEntity, lastBlockTreeNodes);
}
// handle table
else if (line[blankNum] === '|') {
let combineTable = false;
if (lastBlockTreeNodes.length > 0) {
let lastNodeTrim = lastBlockTreeNodes[lastBlockTreeNodes.length - 1].content.trim();
if (lastNodeTrim.length > 0 && lastNodeTrim[0] === '|') {
lastBlockTreeNodes[lastBlockTreeNodes.length - 1].content += '\n' + line;
combineTable = true;
}
}
if (!combineTable) {
let blockTreeNode = {
refBlock: undefined,
content: line,
children: [],
properties: {},
headerLevel: 0,
blankLevel: blankLevel
}
is_first = appendNewBlockTreeNode(blockTreeNode, is_first, blockEntity, lastBlockTreeNodes);
}
}
// handle order list
else if (/^\s*([0-9]+|[A-z]+|[一二三四五六七八九十零]+)[.、.]\s*/.test(line)) {
let blockProperties: { [key: string]: string } = {};
if (!transformerContext.orderedToNonOrdered) {
blockProperties['logseq.order-list-type'] = 'number';
}
let blockTreeNode = {
refBlock: undefined,
content: line.replace(/^\s*([0-9]+|[A-z]+|[一二三四五六七八九十零]+)[.、.]\s*/, ''),
children: [],
properties: blockProperties,
headerLevel: 0,
blankLevel: blankLevel
};
is_first = appendNewBlockTreeNode(blockTreeNode, is_first, blockEntity, lastBlockTreeNodes);
}
// handle normal list
else if (/^\s*[-*]\s/.test(line)) {
let blockTreeNode = {
refBlock: undefined,
content: line.replace(/^\s*[-*]\s/, ''),
children: [],
properties: {},
headerLevel: 0,
blankLevel: blankLevel
};
is_first = appendNewBlockTreeNode(blockTreeNode, is_first, blockEntity, lastBlockTreeNodes);
}
// handle normal line
else {
let blockTreeNode = {
refBlock: undefined,
content: line,
children: [],
properties: {},
headerLevel: 0,
blankLevel: blankLevel
};
is_first = appendNewBlockTreeNode(blockTreeNode, is_first, blockEntity, lastBlockTreeNodes);
}
}
}
// append empty line for empty result
if (outputBlockTreeNodes.length == 0) {
let blockTreeNode = {
refBlock: undefined,
content: '',
children: [],
properties: {},
headerLevel: 0,
blankLevel: 0
}
is_first = appendNewBlockTreeNode(blockTreeNode, is_first, blockEntity, lastBlockTreeNodes);
}
let children = await getBlockEntityChildren(blockEntity);
let childBlockTreeNodes = await splitBlocksToTree(children, transformerContext);
for (let childBlockTreeNode of childBlockTreeNodes) {
lastBlockTreeNodes[0].children.push(childBlockTreeNode)
}
}
return outputBlockTreeNodes;
}
async function getHeaderLevelByParent(blockEntity: BlockEntity): Promise<number> {
let headerLevel = 1;
let currentBlockEntity = blockEntity;
while (currentBlockEntity.parent) {
let parentBlockEntity = await logseq.Editor.getBlock(currentBlockEntity.parent.id);
if (!parentBlockEntity) {
return headerLevel;
}
let match = parentBlockEntity.content.match(/^(#+)\s/);
if (match) {
headerLevel = match[1].length + 1;
return headerLevel;
}
currentBlockEntity = parentBlockEntity;
}
return headerLevel;
}
function getContent(blockEntity: BlockEntity) {
if (!blockEntity.properties) {
return blockEntity.content
}
// let propertiesLines: string[] = []
// Object.entries(blockEntity.properties).forEach(([key, value]) => {
// propertiesLines.push(camelToKebab(key) + ':: ')
// })
let lines = blockEntity.content.split(/\r\n|\n|\r/);
// exclude properties lines by prefix
lines = lines.filter(line =>
!/^\s*[a-z][a-z0-9]*(?:[-.][a-z0-9]+)*:: /i.test(line)
);
console.log(lines)
return lines.join('\n')
}
async function headerModeAction(blockEntities: BlockEntity[], transformerContext: TransformerContext, headerLevel = -1) {
let boldToHeader = transformerContext.boldToHeader;
function test_bold_header(content: string) {
return !/[\r\n]/.test(content) && /^\s*\*\*([^*]+)\*\*[,.:;!?:\s,。:;!?:]*$/.test(content);
}
if (boldToHeader && blockEntities.length > 0) {
const boldBlocks = blockEntities.filter(b => /\*\*(.*)\*\*/.test(getContent(b)));
if (boldBlocks.length > 0) {
const convertibleBoldBlocksCount = boldBlocks.filter(b => {
const content = getContent(b);
return test_bold_header(content);
}).length;
if (convertibleBoldBlocksCount < boldBlocks.length) {
boldToHeader = false;
}
}
}
for (let blockEntity of blockEntities) {
if (headerLevel < 0) {
headerLevel = await getHeaderLevelByParent(blockEntity);
}
headerLevel = Math.min(headerLevel, transformerContext.maxHeaderLevel);
// is header
let content = getContent(blockEntity);
let is_header = !/[\r\n]/.test(content) && /^\s*#+\s/.test(content);
let is_bold_header = boldToHeader && test_bold_header(content)
if (is_header || is_bold_header) {
let newContent = content;
newContent = newContent.replace(/^\s*#+\s/, "");
newContent = newContent.replace(/^\s*\*\*(.*)\*\*/, "$1");
// remove tail punctuation
if (transformerContext.removeTailPunctuation) {
newContent = newContent.replace(/[,.:;!?:\s,。:;!?:]*$/, "");
}
// remove serial number
if (transformerContext.orderedToNonOrdered) {
newContent = newContent.replace(/^\s*([0-9]+|[A-z]+|[一二三四五六七八九十零]+)[.、.]\s*(.*)$/, "$2");
}
// add header by header level
newContent = " " + newContent.trim();
for (let i = 0; i < headerLevel; i++) {
newContent = '#' + newContent;
}
const newProperties = convertBlockProperties(blockEntity.properties);
newProperties.heading = headerLevel;
if (content !== newContent || !isEqual(newProperties, convertBlockProperties(blockEntity.properties))) {
await logseq.Editor.updateBlock(blockEntity.uuid, newContent, {properties: newProperties});
}
}
let children = await getBlockEntityChildren(blockEntity);
await headerModeAction(children, transformerContext, headerLevel + 1);
}
return blockEntities;
}
async function modifyBlockAsTree(originBlocks: BlockEntity[], blockTreeNodes: BlockTreeNode[]) {
let visitContext: VisitContext = {
parentBlock: undefined,
lastVisitedBlock: undefined,
visitedBlockUuids: new Set()
}
for (let blockTreeNode of blockTreeNodes) {
await modifyBlockAsTreeModifyHelper(blockTreeNode, visitContext);
}
// delete unvisited blocks
for (let originBlock of originBlocks) {
if (!visitContext.visitedBlockUuids.has(originBlock.uuid)) {
console.log("delete block", originBlock)
await logseq.Editor.removeBlock(originBlock.uuid);
} else {
await modifyBlockAsTreeDeleteHelper(originBlock, visitContext);
}
}
let newSelectedBlockEntity = [];
for (let blockTreeNode of blockTreeNodes) {
if (blockTreeNode.refBlock) {
newSelectedBlockEntity.push(blockTreeNode.refBlock);
}
}
return newSelectedBlockEntity;
}
async function modifyBlockAsTreeModifyHelper(blockTreeNode: BlockTreeNode, visitContext: VisitContext) {
let current_block: BlockEntity | undefined = undefined;
// insert empty block
if (blockTreeNode.refBlock === undefined && visitContext.lastVisitedBlock?.uuid) {
let newBlock = await logseq.Editor.insertBlock(visitContext.lastVisitedBlock?.uuid, blockTreeNode.content, {
sibling: visitContext.lastVisitedBlock?.uuid !== visitContext.parentBlock?.uuid,
properties: blockTreeNode.properties
}) || undefined;
blockTreeNode.refBlock = newBlock?.uuid && await logseq.Editor.getBlock(newBlock.uuid) || undefined;
console.debug("blockTreeNode.refBlock", blockTreeNode.refBlock)
}
// move and update block
if (blockTreeNode.refBlock?.uuid !== undefined) {
// move block to right position
// if parentBlock undefined,update
// if parentBlock defined,move if parent id or left id not equal
if (visitContext.parentBlock !== undefined && (visitContext.parentBlock.id !== blockTreeNode.refBlock?.id || visitContext.lastVisitedBlock?.id !== blockTreeNode.refBlock?.left.id)) {
// @ts-ignore
await logseq.Editor.moveBlock(blockTreeNode.refBlock?.uuid, visitContext.lastVisitedBlock?.uuid, {
children: visitContext.lastVisitedBlock?.uuid === visitContext.parentBlock.uuid,
before: false
}
);
}
// update block content and properties
if (blockTreeNode.refBlock.content !== blockTreeNode.content || !isEqual(blockTreeNode.refBlock.properties, blockTreeNode.properties)) {
console.debug("update block", blockTreeNode.refBlock, blockTreeNode.content, blockTreeNode.properties)
await logseq.Editor.updateBlock(blockTreeNode.refBlock?.uuid, blockTreeNode.content, {properties: blockTreeNode.properties});
}
current_block = await logseq.Editor.getBlock(blockTreeNode.refBlock?.uuid) || undefined;
}
if (current_block?.uuid) {
visitContext.visitedBlockUuids.add(current_block?.uuid);
}
visitContext.lastVisitedBlock = current_block;
let lastParentBlock = visitContext.parentBlock;
visitContext.parentBlock = current_block;
for (let child of blockTreeNode.children) {
await modifyBlockAsTreeModifyHelper(child, visitContext);
}
visitContext.parentBlock = lastParentBlock;
visitContext.lastVisitedBlock = current_block;
blockTreeNode.refBlock = current_block;
}
async function modifyBlockAsTreeDeleteHelper(blockEntity: BlockEntity, visitContext: VisitContext) {
// delete block
if (!visitContext.visitedBlockUuids.has(blockEntity.uuid)) {
console.log("delete block", blockEntity)
await logseq.Editor.removeBlock(blockEntity.uuid);
}
let children = await getBlockEntityChildren(blockEntity);
for (let child of children) {
await modifyBlockAsTreeDeleteHelper(child, visitContext);
}
}
async function getBlockEntityChildren(blockEntity: BlockEntity): Promise<BlockEntity[]> {
let children: BlockEntity[] = [];
if (blockEntity.children) {
for (let child of blockEntity.children) {
// @ts-ignore
children.push(child)
}
}
return children
}
async function optimizeSelectedBlocks(originSelectedBlocks: Array<BlockEntity> | null) {
let selectedBlocks: BlockEntity[] = [];
if (originSelectedBlocks && originSelectedBlocks.length > 0) {
let visitSet = new Set<string>();
// construct tree
for (let blockEntity of originSelectedBlocks) {
let newBlockEntity = await logseq.Editor.getBlock(blockEntity.id);
newBlockEntity = await buildBlockEntityTree(blockEntity, visitSet);
if (newBlockEntity) {
selectedBlocks.push(newBlockEntity);
}
}
}
return selectedBlocks;
}
export async function exitEditingMode() {
// exit editing mode
// editing mode modify block have bug:cannot update when cursor is at the end
let isEditing = await logseq.Editor.checkEditing();
if (isEditing) {
await logseq.Editor.exitEditingMode(true);
// sleep to prevent ui bug
await new Promise(resolve => setTimeout(resolve, 100));
}
}
export async function getSelectedBlocks() {
let originSelectedBlocks = await logseq.Editor.getSelectedBlocks();
if (!originSelectedBlocks || originSelectedBlocks.length === 0) {
const currentBlock = await logseq.Editor.getCurrentBlock();
if (currentBlock) {
originSelectedBlocks = [currentBlock];
}
}
console.log(originSelectedBlocks)
return await optimizeSelectedBlocks(originSelectedBlocks);
}
function getBlockEntityUUID(blockEntity: BlockEntity | BlockUUIDTuple): BlockUUID {
// 检查 blockEntity 是否为 BlockEntity 类型
if ('uuid' in blockEntity) {
// 如果是 BlockEntity 类型,直接返回 uuid
return blockEntity.uuid;
} else {
// 否则,blockEntity 应该是 BlockUUIDTuple 类型
// 返回元组的第二个元素,即 uuid
return blockEntity[1];
}
}
async function buildBlockEntityTree(blockEntity: BlockEntity | BlockUUIDTuple, visitSet: Set<String>): Promise<BlockEntity | null> {
let newBlockEntity = await logseq.Editor.getBlock(getBlockEntityUUID(blockEntity));
console.log(newBlockEntity)
if (!newBlockEntity || visitSet.has(newBlockEntity.uuid)) {
return null;
}
visitSet.add(newBlockEntity.uuid);
let newChildren = [];
if (newBlockEntity.children) {
for (let child of newBlockEntity.children) {
let newChild = await buildBlockEntityTree(child, visitSet);
if (newChild) {
newChildren.push(newChild);
}
}
}
newBlockEntity.children = newChildren;
return newBlockEntity;
}
async function splitModeAction(selectedBlockEntities: BlockEntity[], transformerContext: TransformerContext) {
let blockTreeNodes = await splitBlocksToTree(selectedBlockEntities, transformerContext)
return await modifyBlockAsTree(selectedBlockEntities, blockTreeNodes);
}
export async function transformAction(selectedBlockEntities: BlockEntity[]) {
const activeModeId = logseq.settings?.activeModeId;
const transformModes = logseq.settings?.transformModes;
if (!transformModes || transformModes.length === 0) {
await logseq.UI.showMsg('No transform modes configured.', 'error');
return;
}
let activeMode = transformModes.find((m: any) => m.id === activeModeId);
if (!activeMode) {
activeMode = transformModes;
}
await logseq.UI.showMsg('start block transformer in transformMode: ' + activeMode.name)
let transformerContext = new TransformerContext();
Object.assign(transformerContext, activeMode);
console.log("selectedBlockEntities", selectedBlockEntities)
if (transformerContext.useSplit) {
selectedBlockEntities = await splitModeAction(selectedBlockEntities, transformerContext);
selectedBlockEntities = await optimizeSelectedBlocks(selectedBlockEntities);
}
if (transformerContext.useHeader) {
await headerModeAction(selectedBlockEntities, transformerContext);
}
}
async function updateHeadingProperty(blockUuid: BlockUUID, level: number) {
if (level > 0) {
await logseq.Editor.upsertBlockProperty(blockUuid, 'heading', level);
} else {
const heading = await logseq.Editor.getBlockProperty(blockUuid, 'heading');
if (heading) {
await logseq.Editor.removeBlockProperty(blockUuid, 'heading');
}
}
}
export async function changeHeadingLevel(level: number) {
const selectedBlocks = await getSelectedBlocks();
if (selectedBlocks && selectedBlocks.length > 0) {
for (const blockEntity of selectedBlocks) {
let content = getContent(blockEntity);
// Remove existing heading
content = content.replace(/^[#\r\n\s]*/, '');
if (level > 0) {
// Add new heading
content = '#'.repeat(level) + ' ' + content;
}
var properties = convertBlockProperties(blockEntity.properties)
if (level > 0) {
properties.heading = level;
} else {
if (properties.heading) {
delete properties.heading;
}
}
await logseq.Editor.updateBlock(blockEntity.uuid, content, {properties: properties});
}
}
}