Skip to content

Commit

Permalink
Merge pull request #121 from timlrx/v2
Browse files Browse the repository at this point in the history
Fix Bleed component and improve contentlayer utils
  • Loading branch information
timlrx authored Jul 25, 2023
2 parents 98d1866 + 705ca83 commit d24a0b1
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-bears-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pliny': patch
---

fix bleed full mode
5 changes: 5 additions & 0 deletions .changeset/hot-olives-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pliny': patch
---

Improve typing of contentlayer utility functions and filter posts only in prod
2 changes: 2 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"good-cheetahs-tap",
"good-knives-tan",
"great-chairs-warn",
"grumpy-bears-sleep",
"hot-olives-tease",
"metal-coats-grab",
"mighty-garlics-appear",
"modern-planes-end",
Expand Down
7 changes: 7 additions & 0 deletions packages/pliny/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# pliny

## 0.1.0-beta.12

### Patch Changes

- 75533d5: fix bleed full mode
- c41211e: Improve typing of contentlayer utility functions and filter posts only in prod

## 0.1.0-beta.11

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/pliny/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pliny",
"description": "Main entry point for pliny components",
"homepage": "https://github.com/timlrx/pliny",
"version": "0.1.0-beta.11",
"version": "0.1.0-beta.12",
"type": "module",
"exports": {
"./*": "./*",
Expand Down
2 changes: 1 addition & 1 deletion packages/pliny/src/ui/Bleed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Bleed = ({ full, children }: BleedProps) => {
return (
<div
className={`relative mt-6 ${
full ? 'left-2/4 right-2/4 mx-[-50vw]' : '-mx-6 md:-mx-8 2xl:-mx-24'
full ? 'ml-[calc(-50vw+50%)] mr-[calc(-50vw+50%)]' : '-mx-6 md:-mx-8 2xl:-mx-24'
}`}
>
{children}
Expand Down
44 changes: 40 additions & 4 deletions packages/pliny/src/utils/contentlayer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Document, MDX } from 'contentlayer/core'

const isProduction = process.env.NODE_ENV === 'production'

export type MDXDocument = Document & { body: MDX }
export type MDXDocumentDate = MDXDocument & {
date: string
Expand All @@ -19,8 +21,26 @@ export function dateSortDesc(a: string, b: string) {
return 0
}

/**
* Sorts a list of MDX documents by date in descending order
*
* @param {MDXDocumentDate[]} allBlogs
* @param {string} [dateKey='date']
* @return {*}
*/
export function sortPosts(allBlogs: MDXDocumentDate[], dateKey: string = 'date') {
return allBlogs.sort((a, b) => dateSortDesc(a[dateKey], b[dateKey]))
}

/**
* Kept for backwards compatibility
* Please use `sortPosts` instead
* @deprecated
* @param {MDXBlog[]} allBlogs
* @return {*}
*/
export function sortedBlogPost(allBlogs: MDXDocumentDate[]) {
return allBlogs.sort((a, b) => dateSortDesc(a.date, b.date))
return sortPosts(allBlogs)
}

type ConvertUndefined<T> = OrNull<{
Expand Down Expand Up @@ -68,10 +88,26 @@ export const omit = <Obj, Keys extends keyof Obj>(obj: Obj, keys: Keys[]): Omit<

export type CoreContent<T> = Omit<T, 'body' | '_raw' | '_id'>

export function coreContent<T extends MDXDocument>(content: T) {
/**
* Omit body, _raw, _id from MDX document and return only the core content
*
* @param {T} content
* @return {*} {CoreContent<T>}
*/
export function coreContent<T extends MDXDocument>(content: T): CoreContent<T> {
return omit(content, ['body', '_raw', '_id'])
}

export function allCoreContent<T extends MDXDocument>(contents: T[]) {
return contents.map((c) => coreContent(c)).filter((c) => !('draft' in c && c.draft === true))
/**
* Omit body, _raw, _id from a list of MDX documents and returns only the core content
* If `NODE_ENV` === "production", it will also filter out any documents with draft: true.
*
* @param {T[]} contents
* @return {*} {CoreContent<T>[]}
*/
export function allCoreContent<T extends MDXDocument>(contents: T[]): CoreContent<T>[] {
const coreContent = contents.map((c) => coreContent(c))
if (isProduction)
return coreContent.filter((c: CoreContent<T>) => !('draft' in c && c.draft === true))
return coreContent
}

0 comments on commit d24a0b1

Please sign in to comment.