Skip to content

Commit e3e325e

Browse files
refactor: extract specialCharsRegex to shared utilities and add JSDoc comments
- Extract duplicated specialCharsRegex constant to packages/shared/src/lib/strings.ts - Update imports in useEmojiAutocomplete.ts and useMarkdownInput.ts to use shared constant - Add JSDoc comments to markdownToHtmlBasic and htmlToMarkdownBasic functions - Fix prettier formatting in RichTextInput.tsx Co-authored-by: Chris Bongers <rebelchris@users.noreply.github.com>
1 parent 841a068 commit e3e325e

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

packages/shared/src/components/fields/RichTextEditor/useEmojiAutocomplete.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
22
import type { Editor } from '@tiptap/react';
33
import { search as emojiSearch } from 'node-emoji';
4+
import { specialCharsRegex } from '../../../lib/strings';
45

56
type EditorRange = { from: number; to: number } | null;
67

7-
const specialCharsRegex = new RegExp(/[^A-Za-z0-9_.]/);
8-
98
interface UseEmojiAutocompleteProps {
109
enabled: boolean;
1110
onOffsetUpdate: (editor: Editor) => void;

packages/shared/src/components/fields/RichTextInput.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,9 +664,7 @@ function RichTextInput(
664664
.insertContent({
665665
type: 'text',
666666
text: linkText,
667-
marks: [
668-
{ type: 'link', attrs: { href: url } },
669-
],
667+
marks: [{ type: 'link', attrs: { href: url } }],
670668
})
671669
.run();
672670
}}

packages/shared/src/hooks/input/useMarkdownInput.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
} from '../../graphql/posts';
5050
import { generateStorageKey, StorageTopic } from '../../lib/storage';
5151
import { storageWrapper } from '../../lib/storageWrapper';
52+
import { specialCharsRegex } from '../../lib/strings';
5253

5354
export enum MarkdownCommand {
5455
Upload = 'upload',
@@ -108,8 +109,6 @@ export const defaultMarkdownCommands = {
108109
gif: true,
109110
};
110111

111-
const specialCharsRegex = new RegExp(/[^A-Za-z0-9_.]/);
112-
113112
export const useMarkdownInput = ({
114113
textareaRef,
115114
postId,

packages/shared/src/lib/markdownConversion.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ const inlineMarkdownToHtml = (value: string): string => {
2929
return result;
3030
};
3131

32+
/**
33+
* Converts markdown to HTML using basic markdown syntax.
34+
* Supports headings, lists (ordered/unordered), code blocks, inline code,
35+
* bold, italic, links, and images.
36+
*
37+
* @param markdown - The markdown string to convert
38+
* @returns HTML string representation of the markdown
39+
*
40+
* @example
41+
* markdownToHtmlBasic('**bold** and _italic_')
42+
* // Returns: '<p><strong>bold</strong> and <em>italic</em></p>'
43+
*/
3244
export const markdownToHtmlBasic = (markdown: string): string => {
3345
if (!markdown) {
3446
return '';
@@ -191,6 +203,18 @@ const serializeList = (node: Element, ordered: boolean): string => {
191203
.join('\n');
192204
};
193205

206+
/**
207+
* Converts HTML to markdown using basic markdown syntax.
208+
* Supports headings, lists (ordered/unordered), code blocks, inline code,
209+
* bold, italic, links, and images.
210+
*
211+
* @param html - The HTML string to convert
212+
* @returns Markdown string representation of the HTML
213+
*
214+
* @example
215+
* htmlToMarkdownBasic('<p><strong>bold</strong> and <em>italic</em></p>')
216+
* // Returns: '**bold** and _italic_'
217+
*/
194218
export const htmlToMarkdownBasic = (html: string): string => {
195219
if (!html) {
196220
return '';

packages/shared/src/lib/strings.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,10 @@ export const stripHtmlTags = (html: string): string => {
113113
export const escapeMarkdown = (text: string): string => {
114114
return text.replace(/[\\`*_{}[\]()#+\-.!|]/g, '\\$&');
115115
};
116+
117+
/**
118+
* Regex to test if a string contains special characters.
119+
* Matches any character that is NOT alphanumeric, underscore, or dot.
120+
* Used for validating user input like emoji shortcuts or mentions.
121+
*/
122+
export const specialCharsRegex = /[^A-Za-z0-9_.]/;

0 commit comments

Comments
 (0)