Skip to content

Commit f108e5f

Browse files
committed
Add insertNewlineContinueMarkupCommand
FEATURE: Add a variant of `insertNewlineContinueMarkup` that supports configuration options. Closes codemirror/dev#1628
1 parent a35e330 commit f108e5f

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ trailing whitespace and list markers are removed from that line.</p>
115115
not be used as the only binding for Enter (even in a Markdown
116116
document, HTML and code regions might use a different language).</p>
117117
</dd>
118+
<dt id="user-content-insertnewlinecontinuemarkupcommand">
119+
<code><strong><a href="#user-content-insertnewlinecontinuemarkupcommand">insertNewlineContinueMarkupCommand</a></strong>(<a id="user-content-insertnewlinecontinuemarkupcommand^config" href="#user-content-insertnewlinecontinuemarkupcommand^config">config</a>&#8288;?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a> = {}) → <a href="https://codemirror.net/docs/ref#state.StateCommand">StateCommand</a></code></dt>
120+
121+
<dd><p>Returns a command like
122+
<a href="#user-content-insertnewlinecontinuemarkup"><code>insertNewlineContinueMarkup</code></a>,
123+
allowing further configuration.</p>
124+
<dl><dt id="user-content-insertnewlinecontinuemarkupcommand^config">
125+
<code><strong><a href="#user-content-insertnewlinecontinuemarkupcommand^config">config</a></strong></code></dt>
126+
127+
<dd><dl><dt id="user-content-insertnewlinecontinuemarkupcommand^config.nontightlists">
128+
<code><strong><a href="#user-content-insertnewlinecontinuemarkupcommand^config.nontightlists">nonTightLists</a></strong>&#8288;?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></dt>
129+
130+
<dd><p>By default, when pressing enter in a blank second item in a
131+
tight (no blank lines between items) list, the command will
132+
insert a blank line above that item, starting a non-tight list.
133+
Set this to false to disable this behavior.</p>
134+
</dd></dl></dd></dl></dd>
118135
<dt id="user-content-deletemarkupbackward">
119136
<code><strong><a href="#user-content-deletemarkupbackward">deleteMarkupBackward</a></strong>: <a href="https://codemirror.net/docs/ref#state.StateCommand">StateCommand</a></code></dt>
120137

src/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const view = new EditorView({
4242

4343
@insertNewlineContinueMarkup
4444

45+
@insertNewlineContinueMarkupCommand
46+
4547
@deleteMarkupBackward
4648

4749
@markdownKeymap

src/commands.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ function normalizeIndent(content: string, state: EditorState) {
9292
return space + content.slice(blank)
9393
}
9494

95-
/// This command, when invoked in Markdown context with cursor
96-
/// selection(s), will create a new line with the markup for
97-
/// blockquotes and lists that were active on the old line. If the
98-
/// cursor was directly after the end of the markup for the old line,
99-
/// trailing whitespace and list markers are removed from that line.
100-
///
101-
/// The command does nothing in non-Markdown context, so it should
102-
/// not be used as the only binding for Enter (even in a Markdown
103-
/// document, HTML and code regions might use a different language).
104-
export const insertNewlineContinueMarkup: StateCommand = ({state, dispatch}) => {
95+
/// Returns a command like
96+
/// [`insertNewlineContinueMarkup`](#lang-markdown.insertNewlineContinueMarkup),
97+
/// allowing further configuration.
98+
export const insertNewlineContinueMarkupCommand = (config: {
99+
/// By default, when pressing enter in a blank second item in a
100+
/// tight (no blank lines between items) list, the command will
101+
/// insert a blank line above that item, starting a non-tight list.
102+
/// Set this to false to disable this behavior.
103+
nonTightLists?: boolean
104+
} = {}): StateCommand => ({state, dispatch}) => {
105105
let tree = syntaxTree(state), {doc} = state
106106
let dont = null, changes = state.changeByRange(range => {
107107
if (!range.empty || !markdownLanguage.isActiveAt(state, range.from, -1) && !markdownLanguage.isActiveAt(state, range.from, 1))
@@ -119,7 +119,8 @@ export const insertNewlineContinueMarkup: StateCommand = ({state, dispatch}) =>
119119
let first = inner.node.firstChild!, second = inner.node.getChild("ListItem", "ListItem")
120120
// Not second item or blank line before: delete a level of markup
121121
if (first.to >= pos || second && second.to < pos ||
122-
line.from > 0 && !/[^\s>]/.test(doc.lineAt(line.from - 1).text)) {
122+
line.from > 0 && !/[^\s>]/.test(doc.lineAt(line.from - 1).text) ||
123+
config.nonTightLists === false) {
123124
let next = context.length > 1 ? context[context.length - 2] : null
124125
let delTo, insert = ""
125126
if (next && next.item) { // Re-add marker for the list at the next level
@@ -172,6 +173,17 @@ export const insertNewlineContinueMarkup: StateCommand = ({state, dispatch}) =>
172173
return true
173174
}
174175

176+
/// This command, when invoked in Markdown context with cursor
177+
/// selection(s), will create a new line with the markup for
178+
/// blockquotes and lists that were active on the old line. If the
179+
/// cursor was directly after the end of the markup for the old line,
180+
/// trailing whitespace and list markers are removed from that line.
181+
///
182+
/// The command does nothing in non-Markdown context, so it should
183+
/// not be used as the only binding for Enter (even in a Markdown
184+
/// document, HTML and code regions might use a different language).
185+
export const insertNewlineContinueMarkup = insertNewlineContinueMarkupCommand()
186+
175187
function isMark(node: SyntaxNode) {
176188
return node.name == "QuoteMark" || node.name == "ListMark"
177189
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {Completion, CompletionContext} from "@codemirror/autocomplete"
55
import {MarkdownExtension, MarkdownParser, parseCode} from "@lezer/markdown"
66
import {html, htmlCompletionSource} from "@codemirror/lang-html"
77
import {commonmarkLanguage, markdownLanguage, mkLang, getCodeParser, headerIndent} from "./markdown"
8-
import {insertNewlineContinueMarkup, deleteMarkupBackward} from "./commands"
9-
export {commonmarkLanguage, markdownLanguage, insertNewlineContinueMarkup, deleteMarkupBackward}
8+
import {insertNewlineContinueMarkup, insertNewlineContinueMarkupCommand, deleteMarkupBackward} from "./commands"
9+
export {commonmarkLanguage, markdownLanguage, insertNewlineContinueMarkup, insertNewlineContinueMarkupCommand, deleteMarkupBackward}
1010

1111
/// A small keymap with Markdown-specific bindings. Binds Enter to
1212
/// [`insertNewlineContinueMarkup`](#lang-markdown.insertNewlineContinueMarkup)

0 commit comments

Comments
 (0)