-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (141 loc) Β· 4.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require('dotenv-expand')(require('dotenv').config())
const puppeteer = require('puppeteer')
const fs = require('fs')
const { dirname, basename } = require('path')
const crypto = require('crypto')
const cheerio = require('cheerio')
const serialize = require('dom-serializer')
// polyfill matchAll for node versions < 12
const matchAll = require('string.prototype.matchall')
matchAll.shim()
const { mergeDeep, cleanFilename, ensureDirExists } = require('./utils')
const rootUrl = 'https://tailwindui.com'
const output = process.env.OUTPUT || './output'
const htmlMode = process.env.HTMLMODE || 'alpine'
const login = async ({ page }) => {
const $ = await downloadPage({ page, url: '/login' })
await page.type('input[type=email]', process.env.EMAIL)
await page.type('input[type=password]', process.env.PASSWORD)
await page.click('button[type=submit]')
const title = await page.title()
return 'Tailwind UI - Official Tailwind CSS Components' === title
}
const downloadPage = async ({ page, url }) => {
await page.goto(`${rootUrl}${url}`)
const html = await page.content()
return cheerio.load(html.trim())
}
const applyTransformers = (transformers, $, options) => {
transformers.forEach((transformer) => transformer($, options))
return $
}
const processComponentPage = async ({ page, url }) => {
const $ = await downloadPage({ page, url })
const navLinks = $('nav a')
const category = $(navLinks[0]).text().trim()
const subCategory = $(navLinks[1]).text().trim()
const section = $('h2').text().trim()
const transformerNames = (process.env.TRANSFORMERS || '').split(',')
const transformers = []
transformerNames.filter(Boolean).forEach((name) => {
transformers.push(require(`./transformers/${name}`))
})
const components = []
const snippets = $('textarea')
console.log(
`π Found ${snippets.length} component${snippets.length === 1 ? '' : 's'}`,
)
for (let i = 0; i < snippets.length; i++) {
const snippet = snippets[i]
const $container = $(snippet.parentNode.parentNode.parentNode)
const title = $('h3', $container).text().trim()
const filename = cleanFilename(title)
const path = `${url}/${filename}`
const hash = crypto.createHash('sha1').update(path).digest('hex')
let code = ''
if (htmlMode === 'alpine') {
const iframe = $container.parent().find('iframe')
const $doc = cheerio.load(iframe.attr('srcdoc'))
const $body = $doc('body')
const $first = $body.children().first()
code = $first.attr('class') === '' ? $first.html() : $body.html()
code = `<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>\n\n${code}`
} else if (htmlMode === 'comments') {
code = $(snippet).text().trim()
}
code = applyTransformers(
transformers,
// @ts-ignore
// cheerio.load(code, { serialize }),
cheerio.load(code),
{
rootUrl,
output,
title,
path,
fs,
},
).html()
const dir = `${output}${dirname(path)}`
ensureDirExists(dir)
components.push({ hash, title, url: `${url}/${filename}.html` })
const filePath = `${dir}/${basename(path)}.html`
console.log(`π Writing ${filename}.html`)
fs.writeFileSync(filePath, code)
}
return {
[category]: {
[subCategory]: {
[section]: {
url: `${url}/index.html`,
components,
},
},
},
}
}
;(async function () {
console.log('π Start!')
try {
ensureDirExists(output)
if (!/alpine|comments/.test(htmlMode)) {
console.log(
`π« Unknown HTMLMODE '${htmlMode}' - should be alpine|comments`,
)
return 1
}
console.log('π Logging into tailwindui.com...')
const browser = await puppeteer.launch({ headless: false })
const page = await browser.newPage()
const success = await login({ page })
if (!success) {
console.log('π« Invalid credentials')
return 1
}
console.log('β
Success!\n')
console.log(`π Output is ${output}`)
const $ = await downloadPage({ page, url: '/components' })
const library = {}
const links = $('.grid a')
const count = process.env.COUNT || links.length
for (let i = 0; i < count; i++) {
const link = links[i]
const url = $(link).attr('href')
console.log(`β³ Processing ${url}...`)
const components = await processComponentPage({ page, url })
mergeDeep(library, components)
console.log()
}
if (process.env.BUILDINDEX === '1') {
console.log(`β³ Building index pages...`)
buildIndexPage(output, library)
console.log()
}
// browser.close()
} catch (e) {
console.error('Error:', e)
return 1
}
console.log('π Done!')
return 0
})()