-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from KwanJunWen/feat/FundingSectionFromPkg
feat(FundingSectionFromPkg): add component
- Loading branch information
Showing
7 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
"Readme" | ||
], | ||
"author": "Daniel Bartholomae <[email protected]> (https://startup-cto.net)", | ||
"funding": "https://github.com/sponsors/dbartholomae", | ||
"license": "MIT", | ||
"homepage": "https://dbartholomae.github.io/jsx-readme", | ||
"repository": "[email protected]:dbartholomae/jsx-readme.git", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* @jsx MD */ | ||
import MD, { render } from "jsx-md"; | ||
|
||
import { FundingSectionFromPkg } from "./FundingSectionFromPkg"; | ||
|
||
describe("FundingSectionFromPkg", () => { | ||
it("renders a 'Show your support' heading", async () => { | ||
const pkg = { | ||
name: "test-package", | ||
funding: "http://example.com/donate", | ||
}; | ||
expect(await render(<FundingSectionFromPkg pkg={pkg} />)).toContain( | ||
"## 🤝 Show your support" | ||
); | ||
}); | ||
|
||
it("renders a different header if title is set", async () => { | ||
const pkg = { | ||
name: "test-package", | ||
funding: "http://example.com/donate", | ||
}; | ||
expect( | ||
await render( | ||
<FundingSectionFromPkg pkg={pkg} title="Show your support" /> | ||
) | ||
).toContain("## Show your support"); | ||
}); | ||
|
||
it("renders the description for the section", async () => { | ||
const pkg = { | ||
name: "test-package", | ||
funding: "http://example.com/donate", | ||
}; | ||
expect(await render(<FundingSectionFromPkg pkg={pkg} />)).toContain( | ||
"Give a ⭐ if this package helped you! You can also support the development of this package by funding." | ||
); | ||
}); | ||
|
||
it("renders the package funding with string format", async () => { | ||
const pkg = { | ||
name: "test-package", | ||
funding: "http://example.com/donate", | ||
}; | ||
expect(await render(<FundingSectionFromPkg pkg={pkg} />)).toContain( | ||
"\n* [example.com](http://example.com/donate)" | ||
); | ||
}); | ||
|
||
it("renders the package funding with object format", async () => { | ||
const pkg = { | ||
name: "test-package", | ||
funding: { | ||
type: "individual", | ||
url: "http://example.com/donate", | ||
}, | ||
}; | ||
expect(await render(<FundingSectionFromPkg pkg={pkg} />)).toContain( | ||
'\n* [example.com](http://example.com/donate "individual")' | ||
); | ||
}); | ||
|
||
it("renders the package funding with combination of string and object format", async () => { | ||
const pkg = { | ||
name: "test-package", | ||
funding: [ | ||
{ | ||
type: "individual", | ||
url: "http://example.com/donate", | ||
}, | ||
"http://example.com/donateAlso", | ||
{ | ||
type: "patreon", | ||
url: "https://www.patreon.com/my-account", | ||
}, | ||
], | ||
}; | ||
expect(await render(<FundingSectionFromPkg pkg={pkg} />)).toContain( | ||
'* [example.com](http://example.com/donate "individual")\n' + | ||
"* [example.com](http://example.com/donateAlso)\n" + | ||
'* [www.patreon.com](https://www.patreon.com/my-account "patreon")\n\n\n' | ||
); | ||
}); | ||
|
||
it("renders nothing if the package has no funding", async () => { | ||
const pkg = { | ||
name: "test-package", | ||
}; | ||
expect(await render(<FundingSectionFromPkg pkg={pkg} />)).toBe(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* @jsx MD */ | ||
import { Component, Link, UnorderedList } from "jsx-md"; | ||
import MD, { Fragment, Heading, LineBreak } from "jsx-md"; | ||
import { Funding, PackageJSON } from "../PackageJSON"; | ||
import { URL } from "url"; | ||
|
||
/** @internal */ | ||
interface Props { | ||
pkg: Readonly<PackageJSON>; | ||
title?: string; | ||
} | ||
|
||
const normalizeFunding = (funding: string | Funding): Funding => { | ||
if (typeof funding === "string") { | ||
return { type: "", url: funding }; | ||
} | ||
return funding; | ||
}; | ||
|
||
/** Displays the descripion from a package.json file. */ | ||
export const FundingSectionFromPkg: Component<Props> = ({ | ||
pkg: { funding }, | ||
title = "🤝 Show your support", | ||
}: Props) => { | ||
if (funding === undefined) { | ||
return null; | ||
} | ||
|
||
const fundings = Array.isArray(funding) | ||
? funding.map(normalizeFunding) | ||
: [normalizeFunding(funding)]; | ||
|
||
return ( | ||
<Fragment> | ||
<Heading level={2}>{title}</Heading> | ||
{ | ||
"Give a ⭐ if this package helped you! You can also support the development of this package by funding." | ||
} | ||
<LineBreak /> | ||
<LineBreak /> | ||
<UnorderedList> | ||
{fundings.map((fund, idx) => ( | ||
<Link key={idx} title={fund.type} to={fund.url}> | ||
{new URL(fund.url).hostname} | ||
</Link> | ||
))} | ||
</UnorderedList> | ||
<LineBreak /> | ||
<LineBreak /> | ||
</Fragment> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters