Skip to content

Commit

Permalink
add plaintext helper
Browse files Browse the repository at this point in the history
  • Loading branch information
genox committed Jun 4, 2023
1 parent 172dae3 commit 5dd98a8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
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",
Expand Down Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions src/components/portabletext/to-plaintext.ts
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');
};
7 changes: 6 additions & 1 deletion src/demo/demo-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TermDefinition } from './components/TermDefinition';
import { Link } from './components/Link';
import { PortableTextComponents } from '../components/portabletext/types';
import { SchnauzerList } from './components/SchnauzerList';
import { portableTextToPlaintext } from '../components/portabletext/to-plaintext';

// Some examples of custom components
const components: PortableTextComponents = {
Expand All @@ -24,9 +25,13 @@ const components: PortableTextComponents = {
export const DemoPage = component$(() => {
return (
<>
<div class={'prose mx-auto mt-5 lg:container'}>
<div class={'prose mx-auto mt-8 lg:container'}>
<PortableText value={blocks} components={components} />
</div>
<div class={'prose mx-auto mt-8 lg:container'}>
<h2>Plaintext</h2>
<p>{portableTextToPlaintext({ value: blocks })}</p>
</div>
<div id={'portals'}></div>
</>
);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { PortableText } from './components/portabletext/portable-text';
export { portableTextToPlaintext } from './components/portabletext/to-plaintext';
export type {
PortableTextCustomComponents,
PortableTextComponent,
Expand Down

0 comments on commit 5dd98a8

Please sign in to comment.