Skip to content

Commit

Permalink
make test pass by also normalizing away empty headings
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas-C committed Jan 22, 2025
1 parent faa666e commit 7414c63
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/editor/src/plugins/heading/headingPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import { Editor, Node, Point, Range, Text, Transforms } from "slate";
import { Editor, Node, Path, Point, Range, Text, Transforms } from "slate";
import { createPlugin } from "../../core/createPlugin";
import { HEADING_ELEMENT_TYPE } from "./headingTypes";
import { isHeadingElement } from "./queries/headingQueries";
Expand All @@ -25,9 +25,18 @@ const onDelete = (editor: Editor, logger: Logger) => {
export const headingPlugin = createPlugin({
type: HEADING_ELEMENT_TYPE,
name: HEADING_ELEMENT_TYPE,
normalize: (editor, node, _path, logger) => {
normalize: (editor, node, path, logger) => {
if (!isHeadingElement(node)) return false;

if (
Node.string(node) === "" &&
(!editor.selection || (Range.isCollapsed(editor.selection) && !Path.isCommon(path, editor.selection.anchor.path)))
) {
logger.log("Removing empty heading that is not selected");
Transforms.unwrapNodes(editor, { at: path });
return true;
}

const boldEntries = Array.from(editor.nodes({ match: (n) => Text.isText(n) && !!n.bold }), (n) => n);
if (boldEntries.length) {
logger.log("Removing bold from nodes within heading.");
Expand Down

0 comments on commit 7414c63

Please sign in to comment.