-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
43 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@tegonal/portabletext-qwik", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A portable text component for Qwik", | ||
"author": "Oliver Studer <[email protected]> (https://tegonal.com)", | ||
"main": "./dist/index.mjs", | ||
|
@@ -56,7 +56,7 @@ | |
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"eslint-plugin-qwik": "latest", | ||
"np": "7.6.1", | ||
"np": "^7.6.1", | ||
"postcss": "^8.4.23", | ||
"prettier": "^2.8.8", | ||
"prettier-plugin-tailwindcss": "^0.3.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { PortableTextProps } from './types'; | ||
|
||
// Credit: https://www.sanity.io/schemas/portable-text-to-plain-text-cc845843 | ||
// https://www.sanity.io/exchange/community/rexxars | ||
|
||
const defaults = { nonTextBehavior: 'remove' }; | ||
|
||
type FnArgs = { | ||
opts?: typeof defaults; | ||
value: PortableTextProps['value']; | ||
}; | ||
|
||
/** | ||
* Convert a Portable block or array to plain text. Strips the structure of all marks and block types. | ||
* @param value | ||
* @param opts | ||
*/ | ||
export const portableTextToPlaintext = ({ value, opts }: FnArgs) => { | ||
const options = Object.assign({}, defaults, opts); | ||
const array = Array.isArray(value) ? value : [value]; | ||
return array | ||
.map((block) => { | ||
if (block._type !== 'block' || !block.children) { | ||
return options.nonTextBehavior === 'remove' ? '' : `[${block._type} block]`; | ||
} | ||
return block.children.map((child: any) => child.text).join(''); | ||
}) | ||
.join('\n\n'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters