{"id":28714643,"url":"https://github.com/yozhikm/draft-regex","last_synced_at":"2025-06-15T01:08:22.709Z","repository":{"id":57152408,"uuid":"116245998","full_name":"YozhikM/draft-regex","owner":"YozhikM","description":"Flexible helpers for draft.js","archived":false,"fork":false,"pushed_at":"2020-06-02T23:54:01.000Z","size":4755,"stargazers_count":19,"open_issues_count":9,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-15T01:08:06.161Z","etag":null,"topics":["draft-js"],"latest_commit_sha":null,"homepage":"https://yozhikm.github.io/draft-regex/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/YozhikM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-04T10:18:47.000Z","updated_at":"2022-01-11T10:29:10.000Z","dependencies_parsed_at":"2022-09-03T17:51:02.620Z","dependency_job_id":null,"html_url":"https://github.com/YozhikM/draft-regex","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/YozhikM/draft-regex","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YozhikM%2Fdraft-regex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YozhikM%2Fdraft-regex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YozhikM%2Fdraft-regex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YozhikM%2Fdraft-regex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/YozhikM","download_url":"https://codeload.github.com/YozhikM/draft-regex/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YozhikM%2Fdraft-regex/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259906193,"owners_count":22929978,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["draft-js"],"created_at":"2025-06-15T01:08:06.139Z","updated_at":"2025-06-15T01:08:22.700Z","avatar_url":"https://github.com/YozhikM.png","language":"JavaScript","readme":"# Draft Regex\n\n[![NPM version](http://img.shields.io/npm/v/draft-regex.svg)](https://www.npmjs.org/package/draft-regex)\n[![npm](https://img.shields.io/npm/dt/draft-regex.svg)](http://www.npmtrends.com/draft-regex)\n[![install size](https://packagephobia.now.sh/badge?p=draft-regex@1.2.3)](https://packagephobia.now.sh/result?p=draft-regex@1.2.3)\n[![Greenkeeper badge](https://badges.greenkeeper.io/YozhikM/draft-regex.svg)](https://greenkeeper.io/)\n![FlowType compatible](https://img.shields.io/badge/flowtype-compatible-brightgreen.svg)\n\nThese helpers are written for Draft.js to improve Editor capabilities.\n\n## Requirements\n\n* [draft-js](https://github.com/facebook/draft-js)\n* [react](https://github.com/facebook/react)\n* [react-dom](https://github.com/facebook/react)\n\n## Getting started\n\n```bash\nnpm install draft-regex\n```\n\nor\n\n```bash\nyarn add draft-regex\n```\n\n## How to use\n\n```js\nimport * as React from 'react';\nimport { EditorState, Editor } from 'draft-js';\nimport { clearEmptyBlocks, clearPastedStyle, replaceTextRegex } from 'draft-regex';\n\ntype State = {\n  editorState: EditorState,\n};\n\nclass MyEditor extends React.Component\u003cvoid, State\u003e {\n  state: State = {\n    editorState: EditorState.createEmpty(),\n  };\n\n  onChange = (editorState: EditorState) =\u003e {\n    this.setState({ editorState: clearEmptyBlocks(editorState) });\n  };\n\n  handlePastedText = (text: ?string, html: ?string, editorState: EditorState) =\u003e {\n    this.setState({ editorState: clearPastedStyle(editorState) });\n  };\n\n  onSave = () =\u003e {\n    const { editorState } = this.state;\n    this.setState({ editorState: replaceTextRegex(editorState) });\n  };\n\n  render() {\n    const { editorState } = this.state;\n    return (\n      \u003c\u003e\n        \u003cEditor\n          editorState={editorState}\n          onChange={this.onChange}\n          handlePastedText={this.handlePastedText}\n        /\u003e\n        \u003cbutton onClick={this.onSave}\u003eSave\u003c/button\u003e\n      \u003c/\u003e\n    );\n  }\n}\n```\n\n## API\n\nAll helpers are taken by EditorState and options as an argument and returned EditorState.\n\n### clearEmptyBlocks\n\nPrevents the ability to add blank lines more than 3 (varies in settings).\n\nA second argument can take a `number` for remove blank lines (number + 1).\n\n```js\nfunction clearEmptyBlocks(editorState: EditorState, maxEmptyLines?: number = 2): EditorState\n```\n\n### replaceTextRegex\n\nApply regular expressions to the entire text, in the process of typing or after copy/pasting text.\n\nA second argument can take an array of rules. `typoRules` is a set of basic rules that you can not use.\n\nAll regular expressions are used once in the entire text. If you use a lot of regular expressions, the performance of the editor can drop noticeably.\n\n```js\nfunction replaceTextRegex(\n  editorState: EditorState,\n  rulesArray?: Array\u003cRule\u003e = typoRules,\n): EditorState\n```\n\nThe rule looks like this: `{ reg: new RegExp(), shift: '' }`\n\n### clearPastedStyle\n\nClears styles of copy/pasted text to those that you have.\n\nA second argument can take an object that can contain `options`.\n\n`blockTypes` is an array of strings that contains all the types of blocks that you use in your editor. This is useful if you want to clear all styles, except those you can already ask yourself.\n\nA list of all types can be found [here](https://draftjs.org/docs/api-reference-content-block.html#content).\n\nIf you do not want to use all six kinds of headings, you can bring the headers to the same view using `shiftHeader`.\n\nThe same applies to lists.\n\n```js\nfunction clearPastedStyle(\n  editorState: EditorState,\n  options?: {\n    blockTypes?: Array\u003cstring\u003e,\n    shiftHeader?: string,\n    shiftList?: string,\n  }\n): EditorState\n```\n\n## Hints\n\nTo improve performance, use `clearPastedStyle` to `handlePastedText` method, and `replaceTextRegex` to save the text editor.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyozhikm%2Fdraft-regex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyozhikm%2Fdraft-regex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyozhikm%2Fdraft-regex/lists"}