forked from ipfs/awesome-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
License: MIT Signed-off-by: Henrique Dias <[email protected]>
- Loading branch information
Showing
12 changed files
with
140 additions
and
236 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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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() |
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
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,3 @@ | ||
{{ define "main" }} | ||
{{ partial "list" . }} | ||
{{ end }} |
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 |
---|---|---|
@@ -1,7 +1,3 @@ | ||
{{ define "main" }} | ||
|
||
{{ partial "list" .Pages }} | ||
{{ partial "indexes/index" }} | ||
|
||
{{ define "main" }} | ||
{{ partial "list" . }} | ||
{{ end }} | ||
|
Oops, something went wrong.