Skip to content

Commit d1a8ed1

Browse files
authored
FEATURE: add horizontal_rule rich editor input rule (#31788)
Adds support to typing `---`, `___` or `***` to create a horizontal rule. Converting when typing `---` is actually written here as an en-dash + `-`, because the typographer replacements extension turns `--` into an en-dash first. `___` and `***` are only triggered after a whitespace, because they could also mean bold+italic.
1 parent 6820622 commit d1a8ed1

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

app/assets/javascripts/discourse/app/static/prosemirror/core/inputrules.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
textblockTypeInputRule,
66
wrappingInputRule,
77
} from "prosemirror-inputrules";
8+
import { TextSelection } from "prosemirror-state";
89

910
export function buildInputRules(extensions, schema, includeDefault = true) {
1011
const rules = [];
@@ -29,6 +30,8 @@ export function buildInputRules(extensions, schema, includeDefault = true) {
2930
markInputRule(/(?:^|(?<!\*))\*([^*]+)\*$/, schema.marks.em),
3031
markInputRule(/(?<=^|\s)_([^_]+)_$/, schema.marks.em),
3132
markInputRule(/`([^`]+)`$/, schema.marks.code),
33+
34+
new InputRule(/^(\u2013-|___\s|\*\*\*\s)$/, horizontalRuleHandler),
3235
]
3336
);
3437
}
@@ -126,3 +129,13 @@ function markInputRule(regexp, markType, getAttrs) {
126129
return tr;
127130
});
128131
}
132+
133+
function horizontalRuleHandler(state, match, start, end) {
134+
const tr = state.tr;
135+
tr.replaceRangeWith(start, end, [
136+
state.schema.nodes.horizontal_rule.create(),
137+
state.schema.nodes.paragraph.create(),
138+
]);
139+
tr.setSelection(TextSelection.create(tr.doc, start + 1));
140+
return tr;
141+
}

spec/system/composer/prosemirror_editor_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ def open_composer_and_toggle_rich_editor
138138
text: "foo ± bar… test??? wow!!! x, y– — a–> b←- c→ d← e←> f←→ ™ ¶",
139139
)
140140
end
141+
142+
it "supports ---, *** or ___ to create a horizontal rule" do
143+
open_composer_and_toggle_rich_editor
144+
composer.type_content("Hey\n---\nThere\n*** Friend\n___ ")
145+
146+
expect(rich).to have_css("hr", count: 3)
147+
end
141148
end
142149

143150
context "with oneboxing" do

0 commit comments

Comments
 (0)