Skip to content

Commit

Permalink
feat: clean up (a lot) (ipfs#247)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
  • Loading branch information
hacdias authored Apr 30, 2019
1 parent 6f15e77 commit a89015d
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 236 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ stroke*.svg
public
dist
/src/resources
/src/data
/src/content
/src/layouts/partials/indexes
/src/static/fonts
/src/static/app.css
/src/static/app.js
6 changes: 0 additions & 6 deletions Makefile

This file was deleted.

34 changes: 16 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.0.0",
"private": true,
"dependencies": {
"ipfs-css": "^0.5.2",
"lunr": "^2.3.0",
"tachyons": "^4.10.0"
"ipfs-css": "^0.12.0",
"lunr": "^2.3.6",
"tachyons": "^4.11.1"
},
"scripts": {
"start": "node ./scripts/dev.js",
Expand All @@ -18,24 +18,22 @@
"build:css": "postcss --no-map --use postcss-import cssnano -o src/static/app.css src/css/*.css",
"build:js": "browserify -g uglifyify src/js/app.js -o src/static/app.js",
"build:data": "node ./scripts/make-data.js",
"build:hugo": "hugo -s src -d ../public --cleanDestinationDir",
"build:minify": "html-minifier --input-dir=./public --output-dir=./public --file-ext=html --collapse-whitespace --remove-comments"
"build:hugo": "hugo -s src -d ../public --cleanDestinationDir --minify --gc"
},
"devDependencies": {
"babel-eslint": "^8.2.6",
"browserify": "^16.2.2",
"chokidar": "^2.0.4",
"cssnano": "^4.0.1",
"ecstatic": "^3.2.1",
"fs-extra": "^6.0.1",
"html-minifier": "^3.5.19",
"hugo-bin": "^0.29.0",
"node-yaml": "^3.1.1",
"npm-run-all": "^4.1.3",
"postcss-cli": "^5.0.1",
"postcss-import": "^11.1.0",
"babel-eslint": "^10.0.1",
"browserify": "^16.2.3",
"chokidar": "^2.1.5",
"cssnano": "^4.1.10",
"ecstatic": "^4.1.2",
"fs-extra": "^7.0.1",
"hugo-bin": "^0.43.4",
"node-yaml": "^3.2.0",
"npm-run-all": "^4.1.5",
"postcss-cli": "^6.1.2",
"postcss-import": "^12.0.1",
"shx": "^0.3.2",
"standard": "^11.0.1",
"standard": "^12.0.1",
"uglifyify": "^5.0.1",
"watch": "^1.0.2"
},
Expand Down
29 changes: 20 additions & 9 deletions scripts/data.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
const fs = require('fs')
const path = require('path')
const { join } = require('path')
const yaml = require('node-yaml')
const { sortAbc } = require('./utils')
const dataDir = path.join(__dirname, '../data')
const { sortAbc, sortInv, slugify } = require('./utils')

const dir = join(__dirname, '../data')
const trimIfExists = (str) => str ? str.trim() : undefined

const files = fs.readdirSync(dataDir)
.map(file => path.join(dataDir, file))
module.exports = fs.readdirSync(dir)
.map(file => join(dir, file))
.map(file => yaml.readSync(file))
.map(file => {
file.content = file.content.map(({ title, description, ...file }) => ({
file.slug = slugify(file.title)
file.type = 'category'

file.content = file.content.map(({ title, description, ...meta }, i) => ({
...meta,
title: trimIfExists(title),
description: trimIfExists(description),
...file
category: file.slug,
color: file.color,
index: i
}))

let sort = (a, b) => sortAbc(a.title, b.title)

if (file.slug === 'articles') {
sort = (a, b) => sortInv(a.date, b.date)
}

file.content = file.content.sort(sort)
return file
})
.sort((a, b) => sortAbc(a.title, b.title))

module.exports = files
142 changes: 33 additions & 109 deletions scripts/make-data.js
Original file line number Diff line number Diff line change
@@ -1,126 +1,50 @@
const lunr = require('lunr')
const fs = require('fs-extra')
const path = require('path')
const { slugify, capitalize, sortAbc } = require('./utils')

const dataDir = path.join(__dirname, '../src/data')
const contentDir = path.join(__dirname, '../src/content')
const indexesDir = path.join(__dirname, '../src/layouts/partials/indexes')

const processDataType = (data) => {
const content = data.content.map(info => {
const { website, ...more } = info

return {
website: website,
categories: [data.title.toLowerCase()],
...more
}
const { join } = require('path')

function getData () {
let data = require('./data')

data.push({
title: 'Awesome IPFS',
slug: '_index',
content: data
.reduce((arr, cat) => arr.concat(cat.content), [])
.map((el, i) => ({
...el,
index: i
}))
})

delete data.content

return {
info: { ...data },
content: content
}
}

const writeContentFile = (data) => {
const basename = slugify(data.title)
const filename = path.join(contentDir, `${basename}.md`)

fs.writeFileSync(filename, JSON.stringify(data))
data.forEach(makeIndex)
return data
}

const makeIndex = (data) => {
const indexes = { 'index': [] }

const checkField = (field, el) => {
if (Array.isArray(el[field])) {
el[field].forEach(t => {
const key = `${field}_${t}`

if (indexes[key]) {
indexes[key].push(el.index)
} else {
indexes[key] = [el.index]
}
})
}
}

data.forEach(el => {
indexes.index.push(el.index)
checkField('tags', el)
checkField('categories', el)
})

data = data.map(({index, title, description = '', tags = [], categories = []}) => ({
function makeIndex (category) {
const data = category.content.map(({ index, title, description = '', tags = [], category = '' }) => ({
ref: index,
data: `${title} ${description} ${tags.join(' ')} ${categories.join(' ')}`
data: `${title} ${description} ${tags.join(' ')} ${category}`
}))

for (const index in indexes) {
const idx = lunr(function () {
this.ref('ref')
this.field('data')

indexes[index].map(i => data[i]).forEach(this.add.bind(this))
})

const file = path.join(indexesDir, index + '.html')
const json = JSON.stringify(idx).replace(`'`, `\\'`)

fs.writeFileSync(file, `<script>var idx = JSON.parse(\`${json}\`);</script>`)
}
category.index = lunr(function () {
this.ref('ref')
this.field('data')
data.forEach(this.add.bind(this))
})
}

const process = () => {
fs.ensureDirSync(dataDir)
fs.ensureDirSync(contentDir)
fs.ensureDirSync(indexesDir)
fs.emptyDirSync(dataDir)
fs.emptyDirSync(contentDir)
fs.emptyDirSync(indexesDir)

let data = []
let types = []
let typesObj = {}

require('./data')
.map(processDataType)
.forEach(({info, content}) => {
types.push(info)
data.push(content)
})
const dir = join(__dirname, '../src/content')
fs.ensureDirSync(dir)
fs.emptyDirSync(dir)

data = data.reduce((a, v) => a.concat(v), [])
.sort((a, b) => sortAbc(a.title, b.title))
.map((v, i) => { v.index = i; return v })
const data = getData()

data.forEach(writeContentFile)
makeIndex(data)

types = types.map(t => {
t.title = capitalize(t.title)
return t
}).sort((a, b) => {
if (a.weight < b.weight) {
return -1
}

if (a.weight > b.weight) {
return 1
}

return 0
}).forEach(type => {
typesObj[type.title.toLowerCase()] = type
})

const pt = path.join(dataDir, 'categories.json')
fs.writeFileSync(pt, JSON.stringify(typesObj))
for (const { index, slug, ...meta } of data) {
const filename = join(dir, slug + '.md')
fs.writeFileSync(filename, `${JSON.stringify(meta)}
<script>var idx = JSON.parse(\`${JSON.stringify(index).replace(`'`, `\\'`)}\`);</script>`)
}
}

process()
10 changes: 2 additions & 8 deletions scripts/make-readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@ const path = require('path')
const files = require('./data')
const readme = path.join(__dirname, '../README.md')
const template = path.join(__dirname, 'readme-template.md')
const { slugify, sortInv, sortAbc } = require('./utils')
const { slugify } = require('./utils')

const toc = files.map(cat => `- [${cat.title}](#${slugify(cat.title)})`).join('\n')

const sections = files.map(category => {
let sort = (a, b) => sortAbc(a.title, b.title)

if (category.title === 'Articles') {
sort = (a, b) => sortInv(a.date, b.date)
}

const content = category.content.sort(sort).map(item => {
const content = category.content.map(item => {
let block = '- '
let mainUrl = ''

Expand Down
5 changes: 1 addition & 4 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ const slugify = (text) => text.toString()
.replace(/^-+/, '')
.replace(/-+$/, '')

const capitalize = (text) => `${text.charAt(0).toUpperCase()}${text.slice(1).toLowerCase()}`

module.exports = {
sort,
sortInv,
sortAbc,
slugify,
capitalize
slugify
}
15 changes: 6 additions & 9 deletions src/layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@
<img alt="IPFS" src="{{ .Site.BaseURL }}/images/logo.png" class="w3 h3 mr3">
<h1 class="f3 white">Awesome IPFS</h1>
</a>

{{ $site := .Site }}
{{ range $name, $taxonomy := .Site.Taxonomies.categories }}
{{ $color := (index $site.Data.categories $name).color }}
{{ $icon := print (index $site.Data.categories $name).icon ".svg" }}
<a href="/categories/{{ $name | urlize }}"
class="flex-grow-1 flex items-center justify-center white no-underline pa2 bg-animate dib bg-{{ $color }}-muted hover-bg-{{ $color }}">
<span class="w2 h2 fill-white mr2">{{ partial (print "icons/" $icon ) }}</span>
{{ $name }}

{{ range (where .Site.Pages "Type" "category") }}
<a href="{{ .RelPermalink }}"
class="flex-grow-1 flex items-center justify-center white no-underline pa2 bg-animate dib bg-{{ .Params.color }}-muted hover-bg-{{ .Params.color }}">
<span class="w2 h2 fill-white mr2">{{ partial (print "icons/" .Params.icon ".svg" ) }}</span>
{{ .Title }}
</a>
{{ end }}
</nav>
Expand Down
9 changes: 0 additions & 9 deletions src/layouts/_default/list.html

This file was deleted.

3 changes: 3 additions & 0 deletions src/layouts/_default/single.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ define "main" }}
{{ partial "list" . }}
{{ end }}
8 changes: 2 additions & 6 deletions src/layouts/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
{{ define "main" }}

{{ partial "list" .Pages }}
{{ partial "indexes/index" }}

{{ define "main" }}
{{ partial "list" . }}
{{ end }}

Loading

0 comments on commit a89015d

Please sign in to comment.