Skip to content

Commit

Permalink
feat!: upgrade to @sanity/ui v2 (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Sep 9, 2024
1 parent 975285a commit 6575f83
Show file tree
Hide file tree
Showing 15 changed files with 14,598 additions and 8,941 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module.exports = {
'react/no-unused-prop-types': 'off',
'react/no-array-index-key': 'off',
'react/display-name': 0,
camelcase: 'off',
'camelcase': 'off',
'react/react-in-jsx-scope': 'off',
},
settings: {
'import/ignore': ['\\.css$', '.*node_modules.*', '.*:.*'],
Expand Down
7 changes: 7 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"github>sanity-io/renovate-config",
"github>sanity-io/renovate-config:studio-v3"
]
}
21 changes: 6 additions & 15 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,12 @@ concurrency:
cancel-in-progress: true

jobs:
log-the-inputs:
name: Log inputs
runs-on: ubuntu-latest
steps:
- run: |
echo "Inputs: $INPUTS"
env:
INPUTS: ${{ toJSON(inputs) }}
build:
runs-on: ubuntu-latest
name: Lint & Build
steps:
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # tag=v3
- uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
cache: npm
node-version: lts/*
Expand Down Expand Up @@ -91,8 +82,8 @@ jobs:
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # tag=v3
- uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
cache: npm
node-version: ${{ matrix.node }}
Expand All @@ -106,12 +97,12 @@ jobs:
runs-on: ubuntu-latest
name: Semantic release
steps:
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # tag=v3
- uses: actions/checkout@v4
with:
# Need to fetch entire commit history to
# analyze every commit since last release
fetch-depth: 0
- uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3
- uses: actions/setup-node@v4
with:
cache: npm
node-version: lts/*
Expand Down
6 changes: 0 additions & 6 deletions .prettierrc

This file was deleted.

111 changes: 56 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
## What is it?

A Markdown editor with preview for Sanity Studio.
A Markdown editor with preview for Sanity Studio.

Supports Github flavored markdown and image uploads.
You can either drag image(s) into the editor or click the bottom bar to bring up a file selector.
Supports Github flavored markdown and image uploads.
You can either drag image(s) into the editor or click the bottom bar to bring up a file selector.
The resulting image URL(s) are inserted with a default width parameter which you can change to your liking using the [Sanity image pipeline parameters](https://www.sanity.io/docs/image-urls).

The current version is a wrapper around [React SimpleMDE (EasyMDE) Markdown Editor](https://github.com/RIP21/react-simplemde-editor#react-simplemde-easymde-markdown-editor),
Expand All @@ -17,6 +17,7 @@ and by extension, [EasyMDE](https://github.com/Ionaru/easy-markdown-editor).
![example.png](./assets/example.png)

## Installation

Install `sanity-plugin-markdown` and `easymde@2` (peer dependency).

```
Expand All @@ -28,75 +29,77 @@ npm install --save sanity-plugin-markdown easymde@2
Add it as a plugin in sanity.config.ts (or .js):

```js
import { markdownSchema } from "sanity-plugin-markdown";
import {markdownSchema} from 'sanity-plugin-markdown'

export default defineConfig({
// ...
plugins: [
markdownSchema(),
]
plugins: [markdownSchema()],
})
```

Then, declare a field in your schema to be `markdown`

```javascript
const myDocument = {
type: "document",
name: "myDocument",
type: 'document',
name: 'myDocument',
fields: [
{
type: "markdown",
description: "A Github flavored markdown field with image uploading",
name: "bio"
}
]
type: 'markdown',
description: 'A Github flavored markdown field with image uploading',
name: 'bio',
},
],
}
```

### Next.js compatability
Next.js *without* Next 13 app directory does not support css imports from `node_modules`.

Next.js _without_ Next 13 app directory does not support css imports from `node_modules`.

To use this plugin in this context (`pages` directory), use the `sanity-plugin-markdown/next` import instead of `sanity-plugin-markdown`:

```js
import { markdownSchema } from "sanity-plugin-markdown/next";
import {markdownSchema} from 'sanity-plugin-markdown/next'
```

Then, make sure to add
Then, make sure to add

```js
import 'easymde/dist/easymde.min.css'
```

to the top of `pages/_app.tsx`.
to the top of `pages/_app.tsx`.

### Customizing the default markdown input editor

The plugin takes an `input` config option that can be used in combination with the `MarkdownInput` export
to configure the underlying React SimpleMDE component:

* Create a custom component that wraps MarkdownInput
* Memoize reactMdeProps and pass along
- Create a custom component that wraps MarkdownInput
- Memoize reactMdeProps and pass along

```tsx
// CustomMarkdownInput.tsx
import { MarkdownInput, MarkdownInputProps } from 'sanity-plugin-markdown'
import {MarkdownInput, MarkdownInputProps} from 'sanity-plugin-markdown'

export function CustomMarkdownInput(props) {
const reactMdeProps: MarkdownInputProps['reactMdeProps'] =
useMemo(() => {
return {
options: {
toolbar: ['bold', 'italic'],
// more options available, see:
// https://github.com/Ionaru/easy-markdown-editor#options-list
},
// more props available, see:
// https://github.com/RIP21/react-simplemde-editor#react-simplemde-easymde-markdown-editor
}
}, [])
const reactMdeProps: MarkdownInputProps['reactMdeProps'] = useMemo(() => {
return {
options: {
toolbar: ['bold', 'italic'],
// more options available, see:
// https://github.com/Ionaru/easy-markdown-editor#options-list
},
// more props available, see:
// https://github.com/RIP21/react-simplemde-editor#react-simplemde-easymde-markdown-editor
}
}, [])

return <MarkdownInput {...props} reactMdeProps={reactMdeProps} />
}
```

Set the plugin input option:

```ts
Expand All @@ -106,9 +109,7 @@ import {CustomMarkdownInput} from './CustomMarkdownInput'

export default defineConfig({
// ... rest of the config
plugins: [
markdownSchema({input: CustomMarkdownInput}),
]
plugins: [markdownSchema({input: CustomMarkdownInput})],
})
```

Expand All @@ -121,13 +122,13 @@ defineField({
type: 'markdown',
name: 'markdown',
title: 'Markdown',
components: {input: CustomMarkdownInput}
components: {input: CustomMarkdownInput},
})
```

### Customizing editor preview

One way to customize the preview that does not involve ReactDOMServer
One way to customize the preview that does not involve ReactDOMServer
(used by React SimpleMDE) is to install [marked](https://github.com/markedjs/marked) and
[DOMPurify](https://github.com/cure53/DOMPurify) and create a custom preview:

Expand All @@ -137,24 +138,23 @@ Then use these to create a custom editor:

```tsx
// MarkdownInputCustomPreview.tsx
import { MarkdownInput, MarkdownInputProps } from 'sanity-plugin-markdown'
import {MarkdownInput, MarkdownInputProps} from 'sanity-plugin-markdown'
import DOMPurify from 'dompurify'
import {marked} from 'marked'

export function CustomMarkdownInput(props) {
const reactMdeProps: MarkdownInputProps['reactMdeProps'] =
useMemo(() => {
return {
options: {
previewRender: (markdownText) => {
// configure as needed according to
// https://github.com/markedjs/marked#docs
return DOMPurify.sanitize(marked.parse(markdownText))
}
//customizing using renderingConfig is also an option
const reactMdeProps: MarkdownInputProps['reactMdeProps'] = useMemo(() => {
return {
options: {
previewRender: (markdownText) => {
// configure as needed according to
// https://github.com/markedjs/marked#docs
return DOMPurify.sanitize(marked.parse(markdownText))
},
}
}, [])
//customizing using renderingConfig is also an option
},
}
}, [])

return <MarkdownInput {...props} reactMdeProps={reactMdeProps} />
}
Expand All @@ -166,12 +166,13 @@ Use the component as described in previous sections.

Provide a function to options.imageUrl that takes a SanityImageAssetDocument and returns a string.

The function will be invoked whenever an image is pasted or dragged into the markdown editor,
The function will be invoked whenever an image is pasted or dragged into the markdown editor,
after upload completes.

The default implementation uses

```js
imageAsset => `${imageAsset.url}?w=450`
;(imageAsset) => `${imageAsset.url}?w=450`
```

#### Example imageUrl option
Expand All @@ -182,8 +183,8 @@ defineField({
name: 'markdown',
title: 'Markdown',
options: {
imageUrl: imageAsset => `${imageAsset.url}?w=400&h=400`
}
imageUrl: (imageAsset) => `${imageAsset.url}?w=400&h=400`,
},
})
```

Expand Down
Loading

0 comments on commit 6575f83

Please sign in to comment.