diff --git a/CHANGELOG.md b/CHANGELOG.md index 9812247..096f0e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,18 @@ All notable changes to this project are documented in this file. Format loosely follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). +## [1.2.1] - 2026-11-11 + +### Fixed + +- **Toggle Heading (strip formatting) left the cursor before the new marker on an + empty line.** Running *Toggle Heading - H1…H6 (strip formatting)* on an empty line + produced `|### ` instead of `### |`: the line was rewritten with `setLine`, which + maps a caret at column 0 to before the inserted text. The line replacement and the + cursor position are now set in a single transaction; the cursor keeps its position + relative to the line's text, also accounting for removed bold/italic markers. + (`src/ToggleHeading.ts`) + ## [1.2.0] - 2026-10-08 ### Added diff --git a/src/ToggleHeading.ts b/src/ToggleHeading.ts index f78b0de..9076f6a 100644 --- a/src/ToggleHeading.ts +++ b/src/ToggleHeading.ts @@ -118,23 +118,53 @@ export class ToggleHeading { const cursor = editor.getCursor() const line = editor.getLine(cursor.line) const match = line.match(/^(#+)\s(.*)/) - + let content = line let currentLevel = 0 - + let oldPrefixLength = 0 + if (match) { currentLevel = match[1].length content = match[2] + oldPrefixLength = line.length - content.length } - - // Strip bold and italic formatting - content = content - .replace(/(\*\*|__)(.*?)\1/g, '$2') - .replace(/(\*|_)(.*?)\1/g, '$2') - - editor.setLine( - cursor.line, - currentLevel === level ? content : '#'.repeat(level) + ' ' + content - ) + + // Cursor offset within the content (a caret inside the old marker + // counts as the start of the content) + let contentCursor = Math.max(0, cursor.ch - oldPrefixLength) + + // Strip bold and italic formatting, shifting the cursor offset left + // by the number of marker characters removed before it + const strip = (text: string, regex: RegExp): string => + text.replace(regex, (whole: string, marker: string, inner: string, offset: number) => { + const closeStart = offset + marker.length + inner.length + if (contentCursor >= closeStart + marker.length) { + contentCursor -= 2 * marker.length + } else if (contentCursor > closeStart) { + contentCursor = closeStart - marker.length + } else if (contentCursor >= offset + marker.length) { + contentCursor -= marker.length + } else if (contentCursor > offset) { + contentCursor = offset + } + return inner + }) + content = strip(content, /(\*\*|__)(.*?)\1/g) + content = strip(content, /(\*|_)(.*?)\1/g) + + const newPrefix = currentLevel === level ? '' : '#'.repeat(level) + ' ' + + // Replace the line and place the cursor in one transaction: a bare + // setLine maps a caret at ch 0 to before the inserted `#… ` prefix + editor.transaction({ + changes: [{ + from: { line: cursor.line, ch: 0 }, + to: { line: cursor.line, ch: line.length }, + text: newPrefix + content, + }], + selection: { + from: { line: cursor.line, ch: newPrefix.length + contentCursor }, + }, + }, 'ToggleHeadingStrip' + level.toString()) } } \ No newline at end of file