Giter Site home page Giter Site logo

igorskyflyer / npm-astro-post-excerpt Goto Github PK

View Code? Open in Web Editor NEW
19.0 3.0 2.0 1.03 MB

⭐ An Astro component that renders post excerpts for your Astro blog - directly from your Markdown and MDX files! πŸ’Ž Featured on Astro's official Integrations library: https://astro.build/integrations?search=igor.dvlpr 😍

Home Page: https://www.npmjs.com/package/@igor.dvlpr/astro-post-excerpt

License: MIT License

Astro 35.85% JavaScript 24.60% TypeScript 2.51% CSS 12.10% MDX 24.94%
astro astrojs component excerpt igorskyflyer javascript js jsx npm post

npm-astro-post-excerpt's Introduction

Logo of Post excerpt for Astro component

Post Excerpt component for πŸš€ Astro


⭐ An Astro component that renders post excerpts for your Astro blog - directly from your Markdown and MDX files. Astro v2+ collections are supported as well! πŸ’Ž
Works with all 4 major versions of Astro and MDX!


Now featured on Astro's official Integrations library 😍



πŸ’– Support further development

I work hard for every project, including this one and your support means a lot to me!
Consider buying me a coffee. β˜•
Thank you for supporting my efforts! πŸ™πŸ˜Š


Donate to igorskyflyer

@igorskyflyer



πŸ•΅πŸΌ Usage


Generated post excerpt
<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: ['astro', '@igor.dvlpr/astro-post-excerpt'],
    },
  }

See #3820 for more information.


If you have migrated your Astro site to v2+ you can now use Collections for your posts loop. This component works with both v2+ and versions prior v2.


If you're upgrading this component while keeping your Astro installation <2, everything should work as expected since the introduced changes are backwards-compatible.


In case of an issue, submit it on the component's Issues page. You should provide a concise explanation of the issue you're facing and sample code - if applicable.


πŸ€ΉπŸΌβ€β™‚οΈ Options

post

post: IAstroMarkdownFile | IAstroCollectionEntry | GlobFile

required , represents the post whose excerpt you want to generate and render. You should insert this component inside your posts loop:

If this prop is not supplied or not valid, the component will throw an error.


Interfaces and types

  • IAstroMarkdownFile,
  • IAstroCollectionEntry,
  • GlobFile

are artificial and they correspond to

  • MarkdownInstance<Record<string, any>>,
  • CollectionEntry<T>,
  • Record<string, any>

respectfully.


words

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

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

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

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

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.



πŸ“ Changelog

✨ Changelog is available here: CHANGELOG.md.


πŸͺͺ License

Licensed under the MIT license which is available here, MIT license.


🧬 Related

@igor.dvlpr/vscode-folderpicker

✨ Provides a custom Folder Picker API + UI for Visual Studio Code. 🎨


@igor.dvlpr/hook

πŸͺ Hooks onto a JavaScript prototype, either extending or changing its behavior or replacing it completely. πŸ‘Ί


@igor.dvlpr/aria

🧬 Meet Aria, an efficient Adblock filter list compiler, with many features that make your maintenance of Adblock filter lists a breeze! πŸ—‘


@igor.dvlpr/scrollend-polyfill

πŸ›΄ A performant and light (< 1KB) JavaScript polyfill for the scrollend Event. ⛸️


@igor.dvlpr/valid-path

🧰 Provides ways of testing whether a given value can be a valid file/directory name. 🏜



Provided by Igor Dimitrijević (@igorskyflyer).

npm-astro-post-excerpt's People

Contributors

bennycode avatar igorskyflyer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

npm-astro-post-excerpt's Issues

Markdown syntax still being rendered

First, thanks for a must needed Astro integration.

I came across an issue in both .mdx and .md files.

The text I get back still has markdown formatting applied to it, at least where italics (*text* or _text_) and bold (**text**) text are concerned. I tried all 3 options for PropRenderer, and they all yielded the same result.

Using Astro 4.2.4.

<PostExcerpt post={post} words={20} addEllipsis={true} headings={PropHeadings.TextOnly} renderer={PropRenderer.Html} />

Example return:

Lorem ipsum dolor **sit amet**, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ![fpo](../../assets/uploads/blog-placeholder-1.jpg) Adipiscing enim…

Missing whitespace when collecting strings

When collecting plain text from MD(x) files there is no whitespace between adjacent strings extracted from adjacent nodes, re-add the whitespace.


Expected

Lorem ipsum. Text in a new element

Actual

Lorem ipsum.Text in a new element

Switch to astro v2

πŸš€ Astro v2 upgrade πŸ‘¨πŸΌβ€πŸš€

  • Upgrade Astro
  • Upgrade Node
  • Move to /src/content
  • Use Collections
  • Remove Markdown plugin configuration
  • Remove Astro.fetchContent()
  • Unify old-style types and v2+ types
  • Tests
  • Release

Boolean prop not valid - aborting. Error

Hello, thanks for your plugin.
Looks great, but unfortunately when trying to implement I'm getting the following error:

Screen Shot 2023-06-01 at 17 35 12

This is my loop for getting the static paths.
./[...page].astro:

export async function getStaticPaths({paginate}) {

    const posts = await getCollection('blog').then(posts =>
	posts
		.map(({ data, slug }) => ({
            slug: slug,
			title: data.title,
			description: data.description,
			pubDate: data.pubDate,
		}))
);
return paginate(posts, {pageSize: 10});


}

const seo = {
  title: 'Blog',
  description: 'Blog posts, essays, diaries, news and tutorials by tjex / Tillman Jex'
}

const { page } = Astro.props;
---


<Layout seo={seo} >
    <article class="blog-page-content__article">
      <section class="postlist">
        {page.data.map((post: any) => <BlogPostPreview post={post} />)}
      </section>
    </article>
    <Paginator page={page}/>
</Layout>

the post prop is beings passed into the BlogPostPreview component.
It's in this component that I want to implement the post excerpt.
../../components/BlogPostPreview.astro:

---
import PostExcerpt from "@igor.dvlpr/astro-post-excerpt";
export interface Props {
    post: any;
}

const { post } = Astro.props;
---

<article class="post-preview">
    <a href={`/blog/${post.slug}`}>
        <h1 class="title">{post.title}</h1>
        <p class="description">{post.description}</p>
    </a>
    <p class="post-excerpt">
        <PostExcerpt {post} words={20} addEllipsis={false} />
    </p>
</article>

You can see here how the blog page looks normally (ie without the post-excerpt implementation): https://tjex.net/blog/

Optionally remove top level headings

I'm wondering if there might be a good way to optionally remove the top level heading from the content to be excerpted. Two fairly common patterns for markdown files is to insert either a

# Heading at the Top of the File

or a

# Heading at the Top of the File

## Followed By a Second Heading

For an except it doesn't make much sense to grab headings, so to be able to at least optionally ignore all headings would be great.

Peer dependency install error

When installing the new version v2.0.0 of this component along with Astro <v2, the peer dependency (= Astro) fails to install due to major version changes.

Make the peerDependency more lenient.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.