rename files

This commit is contained in:
polv committed 2020-12-15 16:27:52 +07:00
1 parent 1fdaf73f14
commit 31dd2b0049
72 files changed
+10320 -31

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
import fs from 'fs'
import path from 'path'
import dotenv from 'dotenv'
dotenv.config({ path: '../../.env' })
dotenv.config()
export const CONTENT_PATH = path.resolve(
process.env.CONTENT_PATH ||
(fs.existsSync('../../_posts') ? '../..' : 'content')
)
export const srcPostPath = (...ps: string[]): string =>
path.join(CONTENT_PATH, '_posts', ...ps)
export const srcMediaPath = (...ps: string[]): string =>
path.join(CONTENT_PATH, '_media', ...ps)
export const builtPath = (...ps: string[]): string =>
path.join(__dirname, '../built', ...ps)
export const dstMediaPath = (...ps: string[]): string =>
path.join(__dirname, '../public/media', ...ps)
+67
View File
@@ -0,0 +1,67 @@
import crypto from 'crypto'
import scopeCss from '@patarapolw/scope-css'
import cheerio from 'cheerio'
import showdown from 'showdown'
export class MakeHTML {
$ = cheerio.load('');
mdConverter = new showdown.Converter({
noHeaderId: true,
parseImgDimensions: true,
simplifiedAutoLink: true,
// literalMidWordUnderscores: true,
strikethrough: true,
tables: true,
tasklists: true,
simpleLineBreaks: true,
openLinksInNewWindow: true,
backslashEscapesHTMLTags: true,
emoji: true,
underline: true,
metadata: true
});
constructor (private slug: string) {
/**
* ! For some reasons default underscore italic fails to render...
*/
this.mdConverter.addExtension(
{
type: 'lang',
regex: /([^\\])_{1}(.*?[^\\])_{1}/g,
replace: '$1<i>$2</i>'
},
'underscore-italic'
)
}
parse (md: string): string {
const id =
'md-' +
crypto
.createHash('sha256')
.update(this.slug || md)
.digest()
.toString('hex')
.substr(8)
const $div = this.$(`<div id="${id}">`)
$div.html(this.mdConverter.makeHtml(md))
$div.find('style').each((_, el) => {
const $el = this.$(el)
$el.html(scopeCss($el.html(), `#${id}`))
})
return this.$('<div>').append($div).html()
}
clean (md: string): string {
const $div = this.$('<div>')
$div.html(this.mdConverter.makeHtml(md))
$div.remove('style')
return $div.html()
}
}
+74
View File
@@ -0,0 +1,74 @@
import fg from 'fast-glob'
import fs from 'fs-extra'
import yaml from 'js-yaml'
import rimraf from 'rimraf'
import * as z from 'zod'
import { Database } from '../assets/schema'
import { builtPath, dstMediaPath, srcMediaPath, srcPostPath } from './dir'
import { MakeHTML } from './make-html'
async function main() {
rimraf.sync(dstMediaPath())
rimraf.sync(builtPath())
await fs.copy(srcMediaPath(), dstMediaPath())
await fs.ensureDir(builtPath())
const db = new Database(builtPath('data.db'))
db.init()
const files = await fg('**/*.md', {
cwd: srcPostPath(),
})
// eslint-disable-next-line array-callback-return
files.map((f) => {
const slug = f.replace(/^.+\//, '').replace(/\.md$/, '')
let header: Record<string, unknown> = {}
let markdown = fs.readFileSync(srcPostPath(f), 'utf8')
if (markdown.startsWith('---\n')) {
const [h, c = ''] = markdown.substr(4).split(/---\n(.*)$/s)
header = yaml.safeLoad(h, {
schema: yaml.JSON_SCHEMA,
}) as Record<string, unknown>
markdown = c
}
const makeHtml = new MakeHTML(slug)
const { title, date, image, tag } = (() => {
const { title, date, image, tag } = header
return z
.object({
title: z.string(),
date: z.date().optional(),
image: z.string().optional(),
tag: z.array(z.string()).optional(),
})
.parse({ title, date, image, tag })
})()
const content = makeHtml.clean(markdown)
const excerptHtml = makeHtml.parse(
markdown.split(/<!-- excerpt(?:_separator)? -->/)[0]
)
const contentHtml = makeHtml.parse(markdown)
db.insert({
slug,
title,
date,
image,
tag,
content,
excerptHtml,
contentHtml,
})
})
}
if (require.main === module) {
main()
}