Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

astro-portabletext

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

astro-portabletext

Render Portable Text with Astro

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.6K
decreased by-23.62%
Maintainers
1
Weekly downloads
 
Created
Source

astro-portabletext

License: ISC

Render Portable Text with Astro.

We have react-portabletext and svelte-portabletext which can be used to output your Portable Text with Astro. Like so...

/* .astro file */
---
import { PortableText } from "@portabletext/react";
---

<PortableText
  /* client:{load|idle|visible|media|only} needed for hydration */
  value={[/* ... */]}
  components={/* ... */}
/>

However, it will add bloat if only one or some of the blocks need hydration.

Install

npm install astro-portabletext --save-dev

Usage

/* .astro file */
---
import { PortableText } from "astro-portabletext";
---

<PortableText 
  value={[/* portable text blocks */]} 
  components={/* custom components */}
/>

Default Components

astro-portabletext components will render the following

{
  /* type: Must be defined by you! */,
  block: {
    h1: /* <h1 class="..."><slot /></h1> */,
    h2: /* <h2 class="..."><slot /></h2> */,
    h3: /* <h3 class="..."><slot /></h3> */,
    h4: /* <h4 class="..."><slot /></h4> */,
    h5: /* <h5 class="..."><slot /></h5> */,
    h6: /* <h6 class="..."><slot /></h6> */,
    blockquote: /* <blockquote class="..."><slot /></blockquote> */,
    normal: /* <p class="..."><slot /></p> */
  },
  list: /* <ul class="..."><slot /></ul> | <ol class="..."><slot /></ol>*/,
  listItem: /* <li class="..."><slot /></li> */,
  mark: {
    code: /* <code class="..."><slot /></code> */,
    em: /* <em class="..."><slot /></em> */,
    link: /* <a href="..." class="..."><slot /></a> */,
    'strike-through': /* <del class="..."><slot /></del> */,
    strong: /* <strong class="..."><slot /></strong> */,
    underline: /* <span class="..." style="text-decoration: underline;"><slot /></span> */
  },
  hardBreak: /* <br /> */,
}

Merge Components

Keep default components and add to them

---
import { PortableText } from "astro-portabletext";
import { Unicorn } from "@component/Unicorn";
import { Dinosaur } from "@component/Dinosaur";
import { Sunny } from "@component/Sunny";
import { Highlight } from "@component/Highlight";
---

<PortableText 
  value={[/* portable text blocks */]}
  components={{
    type: {
      unicorn: Unicorn,
      dinosaur: Dinosaur,
    },
    block: {
      sunny: Sunny,
    },
    mark: {
      highlight: Highlight,
    },
  }}
/>

Overwrite Default Component

Change some default components like so

/* .astro file */
---
import { PortableText } from "astro-portabletext";
import { PageHeading } from "@component/PageHeading";
---

<PortableText 
  value={[/* portable text blocks */]}
  components={{
    block: {
      h1: PageHeading,
    },
  }}
/>

With Handler

Create a handler for better control

/* .astro file */
---
import { PortableText } from "astro-portabletext";
import { Type } from "@handler/Type"
import { Block } from "@handler/Block"
import { Mark } from "@handler/Mark"
---

<PortableText 
  value={[/* portable text blocks */]}
  components={{
    type: Type,
    block: Block,
    mark: Mark
  }}
/>

Using <style> in Astro component

/* .astro file */
---
import { PortableText } from "astro-portabletext";
import { Unicorn } from "@component/Unicorn";
---

<PortableText 
  value={[/* portable text blocks */]} 
  components={{
    type: {
      unicorn: Unicorn
    }
  }}
/>

<style>
  .unicorn {/* some values */}
</style>
/* @component/Unicorn.tsx */
import type { PtTypeComponentProps } from "astro-portabletext";

export function Unicorn(props: PtTypeComponentProps) {
  const { astroClass = "" } = props;

  return (
    <div className={`unicorn ${astroClass}`}>
      /* ... */
    </div>
  )
}

Keywords

FAQs

Package last updated on 06 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc