all repos — snow-editor @ 65993a713586363acceab129af67161a5fbe71cc

small and cozy markdown, and orgmode editor

src/lib/org/editorUtils.js (view raw)

 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
const CHECKLIST_RE = /^(\s*)([-+*])\s+\[([ Xx\-])\](.*)$/;
const HEADING_RE = /^(\*+)\s+(.*)$/;

export function isChecklistLine(line) {
  return CHECKLIST_RE.test(line);
}

export function toggleChecklistLine(line) {
  const match = line.match(CHECKLIST_RE);
  if (!match) return line;

  const [, indent, bullet, state, rest] = match;
  const nextState = state === 'X' || state === 'x' ? ' ' : 'X';
  return `${indent}${bullet} [${nextState}]${rest}`;
}

export function adjustHeading(line, delta) {
  const match = line.match(HEADING_RE);
  if (!match) return line;

  const nextLevel = match[1].length + delta;
  if (nextLevel < 1 || nextLevel > 8) return line;
  return `${'*'.repeat(nextLevel)} ${match[2]}`;
}

export function isHeadingLine(line) {
  return HEADING_RE.test(line);
}

export function insertNewListItem(line) {
  const checklist = line.match(/^(\s*)([-+*])\s+\[[ Xx\-]\](.*)$/);
  if (checklist) {
    return `${checklist[1]}${checklist[2]} [ ]`;
  }

  const list = line.match(/^(\s*)([-+*]|\d+\.)\s+(.*)$/);
  if (list) {
    const bullet = list[2].endsWith('.') ? list[2] : list[2];
    return `${list[1]}${bullet} `;
  }

  if (isHeadingLine(line)) {
    return '- ';
  }

  return '- ';
}