「Markdown 向け、リスト記号 (*) のトグル」の版間の差分

提供:MeryWiki
ナビゲーションに移動 検索に移動
(<source>タグを<syntaxhighlight>タグに置き換える)
(改行の除去)
 
1行目: 1行目:
Markdown のリスト記号をトグル (追加/削除) します。
Markdown のリスト記号をトグル (追加/削除) します。複数行の選択状態で実行すると、複数行に適用できます。
複数行の選択状態で実行すると、複数行に適用できます。


<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">

2023年5月18日 (木) 00:24時点における最新版

Markdown のリスト記号をトグル (追加/削除) します。複数行の選択状態で実行すると、複数行に適用できます。

#title = "リスト記号のトグル";
BeginUndoGroup();

Redraw = false;

header_str = '*';

// 選択状態ではない場合は、カーソル行のみを処理するようにする
if (Document.Selection.IsEmpty) {
  var empty_flg = true;
  // リスト文字の追加/削除を実行した後の位置合わせ用変数
  var margin = 0;
  
  var active_x = Document.Selection.GetActivePointX(mePosLogical);
  var active_y = Document.Selection.GetActivePointY(mePosLogical);
  var top_y = active_y;
  var bottom_y = active_y;
} else {
  var empty_flg = false;
  var top_y = Document.Selection.GetTopPointY(mePosLogical);
  var bottom_y = Document.Selection.GetBottomPointY(mePosLogical);
  
  // 選択範囲の末尾位置が行頭だった場合には、末尾行を対象外とする
  var bottom_x = Document.Selection.GetBottomPointX(mePosLogical);
  if (bottom_x == 1) {
    bottom_y--;
  }
}

for (var i = top_y; i <= bottom_y; i++) {
  var y = i;
  Document.Selection.SetActivePoint(mePosLogical, 1, y);

  // 現在行の内容を取得
  var s = Document.GetLine(y, meGetLineWithNewLines);

  // 行頭の文字を取得 
  var regexp = new RegExp('^(\\s*\\' + header_str + ' +)?', 'i');
  var s_found = s.match(regexp);
  var s_header = '';
  if (s_found) {
    var s_header = s_found[1];
  }

  // 行頭文字がない場合にはリスト記号を追加
  // ある場合にはリスト記号を削除
  if (!s_header) {
    if (empty_flg) {
      margin += 2;
    }
    Document.Selection.StartOfLine(false, mePosLogical);
    Document.Selection.Text = header_str + ' ';
  } else {
    if (empty_flg) {
      margin -= s_header.length;
    }
    Document.Selection.SetAnchorPoint(mePosLogical, 1, y);
    Document.Selection.SetActivePoint(mePosLogical, s_header.length+1, y, true);
    Document.Selection.Delete();
  }
}

if (empty_flg) {
  Document.Selection.SetActivePoint(mePosLogical, active_x + margin, active_y, false)  
} else {
  Document.Selection.SetAnchorPoint(mePosLogical, 1, top_y);
  Document.Selection.SetActivePoint(mePosLogical, 1, bottom_y + 1, true)
}

Redraw = true;
スポンサーリンク