Help create a command that removes "bold" style in markdown or rST without selection


#1

I would like such a command that removes “**” around a piece of text in markdown or restructuredText **without" selecting the text.

For example, your cursor is side a piece of bold text **bold|text**, where ‘|’ marks the position of the cursor. I want a script that checks within a short distance to the cursor (say 20 characters) if there is any “**”. If so, remove them, so I do not need to select the whole text to remove the bold style.

I can toggle bold with selection, as the following, but I don’t know how to remove bold without selection.

atom.commands.add 'atom-text-editor', 'rst:bold-selection', -> # Defines a command to work within the TextEditor.
  return unless editor = atom.workspace.getActiveTextEditor() # Get the active editor object and also return immediately if something goes wrong and there's no active text editor.

  selection = editor.getLastSelection() # Get the most recent selection.
  text = selection.getText() # A selection is an object with a bunch of information attached, so we need to get the text from it

  if text.startsWith("**") && text.endsWith("**")
    selection.insertText(text.substring(2, text.length - 2), {select: true})
  else
    selection.insertText("**" + selection.getText() + "**", {select: true})   # insertText() replaces the whole selection. The options argument makes sure that the resulting text is selected.

#2

Do you also want it to bold the single word if it’s not already bold?


#3

Thanks, not necessarily. For example

**bold t|ext** (| is cursor)

I want the script to explore the vicinity of the cursor a little bit (say 20 characters to the left and to the right). If they find “**” on both sides, then remove them. If there isn’t, just give up.


#4

It’s possible to check the scope for bold directly. Is that better?


#5

That would also be very helpful. Thanks!


#6

This should be a good starting point. It can be extended to check if a selection has been made, and if so do the original behaviour.

It doesn’t handle multiline bold properly either, so that could be improved. It does make sure it only removes ** though, so it won’t do totally crazy stuff with multiline bold.

I know yours was in CoffeeScript, so you’ll have to translate this if you want to stick with it.

atom.commands.add('atom-text-editor', 'rst:bold-selection', () => {
  let editor = atom.workspace.getActiveTextEditor()
  if (editor === undefined) { return; }

  let boldRange = editor.bufferRangeForScopeAtCursor("markup.bold") // only gets the single line; should also check earlier and later lines until no more bold
  if (boldRange) {
    let text = editor.getTextInBufferRange(boldRange)
    if (text.slice(0, 2) === "**") {
      text = text.slice(2)
    }
    if (text.slice(-2) === "**") {
      text = text.slice(0, -2)
    }
    editor.setTextInBufferRange(boldRange, text)
  } else {
    let cursor    = editor.getLastCursor()
    let wordRange = cursor.getCurrentWordBufferRange()
    let text      = editor.getTextInBufferRange(wordRange)
    editor.setTextInBufferRange(wordRange, "**" + text + "**")
  }
})