{"id":13758261,"url":"https://github.com/withspectrum/draft-js-prism-plugin","last_synced_at":"2025-05-10T07:30:46.867Z","repository":{"id":48942993,"uuid":"104352903","full_name":"withspectrum/draft-js-prism-plugin","owner":"withspectrum","description":"Add syntax highlighting support to your DraftJS editor","archived":true,"fork":false,"pushed_at":"2021-07-04T14:22:07.000Z","size":126,"stargazers_count":48,"open_issues_count":6,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-23T00:50:01.446Z","etag":null,"topics":["draft-js","draft-js-plugins","draftjs","draftjs-plugins","prism","prismjs","syntax-highlighting"],"latest_commit_sha":null,"homepage":"","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/withspectrum.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-21T13:39:37.000Z","updated_at":"2024-09-24T18:27:14.000Z","dependencies_parsed_at":"2022-09-24T00:40:11.262Z","dependency_job_id":null,"html_url":"https://github.com/withspectrum/draft-js-prism-plugin","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/withspectrum%2Fdraft-js-prism-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/withspectrum%2Fdraft-js-prism-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/withspectrum%2Fdraft-js-prism-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/withspectrum%2Fdraft-js-prism-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/withspectrum","download_url":"https://codeload.github.com/withspectrum/draft-js-prism-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253379459,"owners_count":21899258,"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","draft-js-plugins","draftjs","draftjs-plugins","prism","prismjs","syntax-highlighting"],"created_at":"2024-08-03T13:00:23.503Z","updated_at":"2025-05-10T07:30:46.523Z","avatar_url":"https://github.com/withspectrum.png","language":"JavaScript","readme":"# `draft-js-prism-plugin`\n\nA DraftJS plugin to add syntax highlighting support to your code blocks. Use in combination with [`draft-js-plugins`](https://github.com/draft-js-plugins/draft-js-plugins).\n\n## Usage\n\nFirst, create the plugin and add it to the `plugins` array of your PluginsEditor:\n\n```JS\nimport Prism from 'prismjs';\nimport createPrismPlugin from 'draft-js-prism-plugin';\nimport \"prismjs/themes/prism.css\"; // add prism.css to add highlights  \n\nclass MyEditor extends React.Component {\n  constructor(props) {\n    super(props);\n\n    const prismPlugin = createPrismPlugin({\n      // It's required to provide your own instance of Prism\n      prism: Prism\n    });\n\n    this.state = {\n      plugins: [prismPlugin]\n    }\n  }\n\n  render() {\n    return (\n      \u003cPluginsEditor\n        plugins={this.state.plugins}\n      /\u003e\n    )\n  }\n}\n```\n\nNow add a `language` key to the data of the code block you want to highlight:\n\n```JS\n// TODO: Somehow get a code block and its key, this is up to you\nconst { block, key } = getCurrentBlock();\nif (block.getType() !== \"code-block\") return;\n\n// Replace the code block with a new one with the data.language changed to \"javascript\"\nconst data = block.getData().merge({ language: 'javascript' });\nconst newBlock = block.merge({ data });\nconst newContentState = currentContent.merge({\n  blockMap: blockMap.set(key, newBlock),\n  selectionAfter: currentSelection\n})\n\n// Now that code block will be highlighted as JavaScript!\nthis.setState({\n  editorState: EditorState.push(editorState, newContentState, \"change-block-data\")\n})\n```\n\n## License\n\nThis code uses the [`draft-js-prism` decorator](https://github.com/SamyPesse/draft-js-prism) by [@SamyPesse](https://github.com/SamyPesse) and is based on code extracted from the [`draft-js-markdown-shortcuts-plugin`](https://github.com/ngs/draft-js-markdown-shortcuts-plugin) by [@ngs](https://github.com/ngs).\n\nLicensed under the MIT License, Copyright ©️  2017 Space Program Inc. See [LICENSE.md](LICENSE.md) for more information.\n","funding_links":[],"categories":["Plugins and Decorators Built for Draft.js","JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwithspectrum%2Fdraft-js-prism-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwithspectrum%2Fdraft-js-prism-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwithspectrum%2Fdraft-js-prism-plugin/lists"}