-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
808 additions
and
188 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 was deleted.
Oops, something went wrong.
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,56 @@ | ||
import { generate } from './generate'; | ||
import { expect } from 'earljs'; | ||
|
||
it('get category exact match', async () => { | ||
const items = [ | ||
{ | ||
name: 'Card', | ||
link: 'https://tailwindcomponents.com/component/card-1', | ||
}, | ||
]; | ||
const result = await generate({ items }); | ||
expect(result).toEqual( | ||
expect.stringMatching('\\* Card - https://tailwindcomponents.com/component/card-1'), | ||
); | ||
}); | ||
|
||
it('get category from word', async () => { | ||
const items = [ | ||
{ | ||
name: 'curvy card', | ||
link: 'https://example.com', | ||
}, | ||
]; | ||
const result = await generate({ items }); | ||
expect(result).toEqual(expect.stringMatching('\\* curvy card - https://example.com')); | ||
}); | ||
|
||
it('get category exact pluralize', async () => { | ||
const items = [ | ||
{ | ||
name: 'Cards', | ||
link: 'https://example.com', | ||
}, | ||
]; | ||
const result = await generate({ items }); | ||
expect(result).toEqual(expect.stringMatching('## Card\n\\* Cards - https://example.com')); | ||
}); | ||
|
||
it('sorted alphabetically', async () => { | ||
const items = [ | ||
{ | ||
name: 'Cards', | ||
link: 'https://example.com', | ||
}, | ||
{ | ||
name: 'Button', | ||
link: 'https://example.com', | ||
}, | ||
]; | ||
const result = await generate({ items }); | ||
expect(result).toEqual( | ||
expect.stringMatching( | ||
'## Button\n\\* Button - https://example.com\n## Card\n\\* Cards - https://example.com', | ||
), | ||
); | ||
}); |
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,70 @@ | ||
import { CompomentLink } from './types'; | ||
import stringSimilarity from 'string-similarity'; | ||
import _ from 'lodash'; | ||
import { plural } from 'pluralize'; | ||
|
||
const categoryList = new Map([ | ||
['Alert', ['toast']], | ||
['Avatar', []], | ||
['Breadcrumb', []], | ||
['Button', []], | ||
['Badge', []], | ||
['Card', []], | ||
['Dropdown', []], | ||
['Form', []], | ||
['Hero', []], | ||
['Layout', []], | ||
['Modal', []], | ||
['Navigation', ['navbar']], | ||
['Pagination', []], | ||
['Sidebar', ['side panel']], | ||
['Step', []], | ||
['Section', []], | ||
['Switch', ['toggle']], | ||
['Table', []], | ||
['Tab', []], | ||
['Other', []], | ||
]); | ||
|
||
type GenerateArgs = { | ||
items: CompomentLink[]; | ||
}; | ||
|
||
export async function generate({ items }: GenerateArgs) { | ||
const byCategory = _(items) | ||
.groupBy((item) => getCategory(item.category || item.name)) | ||
.toPairs() | ||
.sortBy(0) | ||
.fromPairs() | ||
.value(); | ||
const content: string[] = []; | ||
for (const [category, items] of Object.entries(byCategory)) { | ||
content.push(`## ${category}`); | ||
content.push(items.map(createLink).join('\n')); | ||
} | ||
return `# Tailwind Components\n## Table of Contents\n${content.join('\n')}`; | ||
} | ||
|
||
function getCategory(name: string) { | ||
for (const [category, keywords] of categoryList.entries()) { | ||
const categoryLower = category.toLowerCase(); | ||
const categoryPlural = plural(categoryLower); | ||
if (categoryLower === name.toLowerCase()) { | ||
return category; | ||
} | ||
const words = name.split(/\s+/); | ||
if ( | ||
words.find((word) => { | ||
const wordLower = word.toLowerCase(); | ||
return wordLower === categoryLower || categoryPlural === plural(wordLower); | ||
}) | ||
) { | ||
return category; | ||
} | ||
} | ||
return 'Other'; | ||
} | ||
|
||
function createLink(item: CompomentLink) { | ||
return `* ${item.name} - ${item.link}`; | ||
} |
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,20 @@ | ||
import puppeteer from 'puppeteer'; | ||
import { getScrapers } from './scrapers'; | ||
import { CompomentLink } from './types'; | ||
import { generate } from './generate'; | ||
import { promises as fs } from 'fs'; | ||
|
||
async function main() { | ||
const browser = await puppeteer.launch({ headless: false, slowMo: 0 }); | ||
const [page] = await browser.pages(); | ||
const scrapers = await getScrapers(); | ||
const items: CompomentLink[] = []; | ||
for await (const scraper of scrapers) { | ||
items.push(...(await scraper({ page }))); | ||
} | ||
await browser.close(); | ||
const content = await generate({ items }); | ||
await fs.writeFile('README.md', content); | ||
} | ||
|
||
main(); |
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,4 @@ | ||
export async function getScrapers() { | ||
const result = Promise.all([import('./tailwindcomponents').then((m) => m.default)]); | ||
return result; | ||
} |
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,31 @@ | ||
import { CompomentLink, ScraperArgs } from '../types'; | ||
|
||
export default async function tailwindcomponents({ page }: ScraperArgs) { | ||
await page.goto('https://tailwindcomponents.com/components'); | ||
const itemSelector = '#app > .min-h-screen > main > div a.flex.flex-col'; | ||
const nextPageSelector = 'a[title="Next »"]'; | ||
const items: CompomentLink[] = await page.$$eval(itemSelector, (elements) => { | ||
return elements.map((a) => ({ | ||
name: a.querySelector('h4')!.textContent as string, | ||
link: `https://tailwindcomponents.com${a.getAttribute('href')}`, | ||
})); | ||
}); | ||
let nextPage: string | null = ''; | ||
|
||
do { | ||
if (!(await page.$(nextPageSelector))) { | ||
break; | ||
} | ||
nextPage = await page.$eval(nextPageSelector, (a) => a.getAttribute('href')); | ||
await page.goto(nextPage!); | ||
const nextItems = await page.$$eval(itemSelector, (elements) => { | ||
return elements.map((a) => ({ | ||
name: a.querySelector('h4')!.textContent as string, | ||
link: `https://tailwindcomponents.com${a.getAttribute('href')}`, | ||
})); | ||
}); | ||
items.push(...nextItems); | ||
} while (nextPage); | ||
|
||
return items; | ||
} |
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,11 @@ | ||
import { Page } from 'puppeteer'; | ||
|
||
export type ScraperArgs = { | ||
page: Page; | ||
}; | ||
|
||
export type CompomentLink = { | ||
category?: string; | ||
name: string; | ||
link: string; | ||
}; |
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