Skip to content

Commit

Permalink
Exports transformEditorContent function which can transform html stri…
Browse files Browse the repository at this point in the history
…ng to editor supported format (#916)
  • Loading branch information
gaagul authored Oct 19, 2023
1 parent 94d1d73 commit aaab187
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/components/Editor/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ export const EDITOR_PADDING_SIZE = 12;
export const EDITOR_BORDER_SIZE = 1;

export const EDITOR_LINE_HEIGHT = 21;

export const IMAGE_REGEX = new RegExp(/(<img[^>]*?>)(?![\s\S]*<\/figure>)/g);
export const IMAGE_REPLACEMENT_PATTERN = "<figure>$1</figure>";
export const EMPTY_DIV_REGEX = new RegExp(
/<div[^>]*?>\s*(?:<br[^>]*?>)\s*<\/div>/g
);
export const TRAILING_BR_REGEX = new RegExp(/\s*(?:<br[^>]*?>)+\s*$/);
10 changes: 10 additions & 0 deletions src/components/Editor/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
EDITOR_LINE_HEIGHT,
EDITOR_BORDER_SIZE,
EDITOR_PADDING_SIZE,
IMAGE_REGEX,
IMAGE_REPLACEMENT_PATTERN,
EMPTY_DIV_REGEX,
TRAILING_BR_REGEX,
} from "./constants";

export const getEditorStyles = ({ rows }) => {
Expand Down Expand Up @@ -62,3 +66,9 @@ export const validateAndFormatUrl = url => {

return url;
};

export const transformEditorContent = content =>
content
?.replaceAll(IMAGE_REGEX, IMAGE_REPLACEMENT_PATTERN)
?.replaceAll(EMPTY_DIV_REGEX, "")
?.replace(TRAILING_BR_REGEX, "");
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { isEditorOverlaysActive } from "components/Editor/utils";
import {
isEditorOverlaysActive,
transformEditorContent,
} from "components/Editor/utils";
import { substituteVariables } from "components/EditorContent/utils";
import { isEditorEmpty } from "utils/common";
import { initializeI18n } from "utils/i18next";
Expand All @@ -22,4 +25,5 @@ export {
FormikEditor,
Attachments,
isEditorOverlaysActive,
transformEditorContent,
};
4 changes: 4 additions & 0 deletions stories/API-Reference/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export const EDITOR_METHODS_TABLE_ROWS = [
"isEditorOverlaysActive",
"Returns a boolean value indicating whether the editor overlays are active or not.",
],
[
"transformEditorContent",
"Modifies the editor's content by performing specific operations, such as replacing <img> tags with <figure> tags, removing empty <div> tags containing line breaks, and eliminating trailing line breaks."
]
];

export const EDITOR_PROPS = [
Expand Down
2 changes: 2 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ export function isEditorEmpty(htmlContent: string | null | undefined): boolean;

export function isEditorOverlaysActive(): boolean;

export function transformEditorContent(content: string): string;

export function substituteVariables(
highlightedContent: string,
variables: (VariableCategory | Variable)[]
Expand Down

0 comments on commit aaab187

Please sign in to comment.