feat: pluralize things in lists

This commit is contained in:
Jacky Zhao 2023-09-02 18:07:26 -07:00
parent 53dd86727b
commit c7cd941e5f
3 changed files with 12 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import style from "../styles/listPage.scss"
import { PageList } from "../PageList" import { PageList } from "../PageList"
import { _stripSlashes, simplifySlug } from "../../util/path" import { _stripSlashes, simplifySlug } from "../../util/path"
import { Root } from "hast" import { Root } from "hast"
import { pluralize } from "../../util/lang"
function FolderContent(props: QuartzComponentProps) { function FolderContent(props: QuartzComponentProps) {
const { tree, fileData, allFiles } = props const { tree, fileData, allFiles } = props
@ -36,7 +37,7 @@ function FolderContent(props: QuartzComponentProps) {
<article> <article>
<p>{content}</p> <p>{content}</p>
</article> </article>
<p>{allPagesInFolder.length} items under this folder.</p> <p>{pluralize(allPagesInFolder.length, "item")} under this folder.</p>
<div> <div>
<PageList {...listProps} /> <PageList {...listProps} />
</div> </div>

View File

@ -6,6 +6,7 @@ import { PageList } from "../PageList"
import { FullSlug, getAllSegmentPrefixes, simplifySlug } from "../../util/path" import { FullSlug, getAllSegmentPrefixes, simplifySlug } from "../../util/path"
import { QuartzPluginData } from "../../plugins/vfile" import { QuartzPluginData } from "../../plugins/vfile"
import { Root } from "hast" import { Root } from "hast"
import { pluralize } from "../../util/lang"
const numPages = 10 const numPages = 10
function TagContent(props: QuartzComponentProps) { function TagContent(props: QuartzComponentProps) {
@ -60,7 +61,7 @@ function TagContent(props: QuartzComponentProps) {
</h2> </h2>
{content && <p>{content}</p>} {content && <p>{content}</p>}
<p> <p>
{pages.length} items with this tag.{" "} {pluralize(pages.length, "item")} with this tag.{" "}
{pages.length > numPages && `Showing first ${numPages}.`} {pages.length > numPages && `Showing first ${numPages}.`}
</p> </p>
<PageList limit={numPages} {...listProps} /> <PageList limit={numPages} {...listProps} />
@ -80,7 +81,7 @@ function TagContent(props: QuartzComponentProps) {
return ( return (
<div class="popover-hint"> <div class="popover-hint">
<article>{content}</article> <article>{content}</article>
<p>{pages.length} items with this tag.</p> <p>{pluralize(pages.length, "item")} with this tag.</p>
<div> <div>
<PageList {...listProps} /> <PageList {...listProps} />
</div> </div>

7
quartz/util/lang.ts Normal file
View File

@ -0,0 +1,7 @@
export function pluralize(count: number, s: string): string {
if (count === 1) {
return `1 ${s}`
} else {
return `${count} ${s}s`
}
}