Requires Astro
v5+
.
Now featured in Astro's official Integrations library. π
βπΌWhat every blog excerpt wishes it could be.βοΈ
- π Plug-and-play with Astro props
- π Parses Markdown/MDX safely
- π§Ή Strips YAML front matter
- π¬ Preserves whitespace
- βοΈ Trims by word and/or length
- π€ Adds ellipsis smartly
- π§ Configurable props
- π« No client-side JS
- π§© No client-side HTML - output only string
- π§ͺ Warns on invalid input + has fallbacks
<PostExcerpt />
component generated excerpt
Install it by executing:
npm i -D "@igor.dvlpr/astro-post-excerpt"
then import it into your project:
PostPreview.astro
import PostExcerpt from '@igor.dvlpr/astro-post-excerpt'
const { post } = Astro.props
<article>
{ /* other markup */ }
<p class="post-excerpt">
<PostExcerpt
post={post}
words={20}
addEllipsis={false} />
</p>
{ /* other markup */ }
</article>
β NOTE: if you get a build error "unknown file extension ".astro" for @igor.dvlpr/astro-post-excerpt", you'll need to update your
astro.config.mjs
file and add the following property:
vite: {
ssr: {
noExternal: ['@igor.dvlpr/astro-post-excerpt'],
},
}
See #3820 for more information.
post: IAstroCollectionEntry
required , represents the post whose excerpt you want to generate and render. You should insert this component inside your posts loop, obtained via: getCollection()
.
If this prop is not supplied or not valid, the component will throw a fatal
error.
words?: number
optional, represents the number of words the generated excerpt should have in total. Be aware that the prop maxLength
has precedence over this prop but you can use both props simultaneously.
Defaults to 40.
If this prop is not supplied or not valid, a warning will be output to the console.
maxLength?: number
optional, represent the max length of the generated excerpt. Be aware that this prop has precedence over the words
prop but you can use both props simultaneously.
Defaults to 0 (unlimited).
If this prop is not supplied or not valid, a warning will be output to the console.
addEllipsis?: boolean
optional, controls whether an ellipsis should be appended to the post excerpt. Defaults to true.
If this prop is not valid, a warning will be output to the console.
smartEllipsis?: boolean
optional, controls whether the appended ellipsis should be aware of how the excerpt ends, i.e. if the excerpt ends with a punctuation symbol the ellipsis won't be appended. Defaults to true.
Note: punctuation symbols that this component is aware of are:
"."
","
"?"
"!"
";"
"β¦"
If this prop is not valid, a warning will be output to the console.
ellipsis?: string
optional, controls the ellipsis string that should be appended to the post excerpt. Defaults to "β¦".
If this prop is not valid, a warning will be output to the console.
π The changelog is available here, CHANGELOG.md.
Licensed under the MIT license which is available here, MIT license.
Consider buying me a coffee. β

Thank you for supporting my efforts! ππ
π― A utility that lets you manipulate HTML elements, their attributes and innerHTML as strings, on the go and then render the modified HTML. Very useful in SSG projects. π€
@igor.dvlpr/astro-escaped-component
ππ»ββοΈββ‘οΈ An Astro component that holds only HTML-encoded content. π
@igor.dvlpr/astro-saferesource
π Adds CSP-compliant inline scripts and styles to Astro! π
@igor.dvlpr/astro-easynav-button
π§ Add an easy-to-use navigational button (jump to top/bottom) to your Astro site. πΌ
@igor.dvlpr/vscode-folderpicker
β¨ Provides a custom Folder Picker API + UI for Visual Studio Code. π¨
Created by Igor DimitrijeviΔ (@igorskyflyer).