remove pug
This commit is contained in:
1 parent
e397c9ae1e
commit
68840c9c60
49 files changed
+1421
-806
No files matched your search
@@ -14,12 +14,6 @@ export const CONTENT_PATH = path.resolve(
|
||||
)
|
||||
|
||||
export const srcPath = (
|
||||
root: '_post' | '_media' | typeof THEME_FILENAME,
|
||||
root: '_posts' | '_media' | typeof THEME_FILENAME,
|
||||
...ps: string[]
|
||||
): string => path.join(CONTENT_PATH, root, ...ps)
|
||||
|
||||
export const builtPath = (...ps: string[]): string =>
|
||||
path.join(__dirname, '../built', ...ps)
|
||||
|
||||
export const dstMediaPath = (...ps: string[]): string =>
|
||||
path.join(__dirname, '../static/media', ...ps)
|
||||
@@ -2,6 +2,7 @@ import crypto from 'crypto'
|
||||
|
||||
import scopeCss from '@patarapolw/scope-css'
|
||||
import cheerio from 'cheerio'
|
||||
import ejs from 'ejs'
|
||||
import jieba from 'nodejieba'
|
||||
import showdown from 'showdown'
|
||||
|
||||
@@ -16,7 +17,7 @@ export class MakeHTML {
|
||||
strikethrough: true,
|
||||
tables: true,
|
||||
tasklists: true,
|
||||
simpleLineBreaks: true,
|
||||
// simpleLineBreaks: true,
|
||||
openLinksInNewWindow: true,
|
||||
backslashEscapesHTMLTags: true,
|
||||
emoji: true,
|
||||
@@ -24,7 +25,7 @@ export class MakeHTML {
|
||||
metadata: true,
|
||||
})
|
||||
|
||||
constructor(private slug: string) {
|
||||
constructor() {
|
||||
/**
|
||||
* ! For some reasons default underscore italic fails to render...
|
||||
*/
|
||||
@@ -38,18 +39,24 @@ export class MakeHTML {
|
||||
)
|
||||
}
|
||||
|
||||
parse(md: string): string {
|
||||
async parse(
|
||||
filename: string
|
||||
): Promise<{
|
||||
$: cheerio.Cheerio
|
||||
html(): string
|
||||
clean(): string
|
||||
}> {
|
||||
const id =
|
||||
'md-' +
|
||||
crypto
|
||||
.createHash('sha256')
|
||||
.update(this.slug || md)
|
||||
.update(filename)
|
||||
.digest()
|
||||
.toString('hex')
|
||||
.substr(8)
|
||||
|
||||
const $div = this.$(`<div id="${id}">`)
|
||||
$div.html(this.mdConverter.makeHtml(md))
|
||||
$div.html(await this.getHTML(filename))
|
||||
$div.find('style').each((_, el) => {
|
||||
const $el = this.$(el)
|
||||
const html = $el.html() || ''
|
||||
@@ -58,14 +65,34 @@ export class MakeHTML {
|
||||
}
|
||||
})
|
||||
|
||||
return this.$('<div>').append($div).html() || ''
|
||||
return {
|
||||
$: $div,
|
||||
html() {
|
||||
return $div.html() || ''
|
||||
},
|
||||
clean() {
|
||||
return jieba.cutForSearch($div.text() || '').join(' ')
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
clean(md: string): string {
|
||||
const $div = this.$('<div>')
|
||||
$div.html(this.mdConverter.makeHtml(md))
|
||||
$div.remove('style')
|
||||
|
||||
return jieba.cutForSearch($div.html() || '').join(' ')
|
||||
private async getHTML(filename: string) {
|
||||
return this.mdConverter.makeHtml(
|
||||
await ejs.renderFile(filename, {
|
||||
youtube: (id: string) =>
|
||||
this.$('<div>')
|
||||
.html(
|
||||
/* html */ `
|
||||
<div style="padding-top:56.25%">
|
||||
<iframe src="https://www.youtube.com/embed/${id}"
|
||||
frameborder="0"
|
||||
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
|
||||
allowfullscreen
|
||||
style="width:100%; height:100%"></iframe>
|
||||
</div>`
|
||||
)
|
||||
.html(),
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,9 @@ import yaml from 'js-yaml'
|
||||
import rimraf from 'rimraf'
|
||||
|
||||
import { Database, IMatter } from '../server/db'
|
||||
import { builtPath, dstMediaPath } from '../server/dir'
|
||||
import { THEME_FILENAME } from '../server/theme'
|
||||
import { builtPath, dstMediaPath, srcPath } from './dir'
|
||||
import { srcPath } from './dir'
|
||||
import { MakeHTML } from './make-html'
|
||||
|
||||
async function main() {
|
||||
@@ -17,40 +18,38 @@ async function main() {
|
||||
|
||||
await fs.copyFile(srcPath(THEME_FILENAME), builtPath(THEME_FILENAME))
|
||||
|
||||
const db = new Database(builtPath('data.db'))
|
||||
const db = new Database()
|
||||
db.init()
|
||||
|
||||
const files = await fg('**/*.md', {
|
||||
cwd: srcPath('_post'),
|
||||
cwd: srcPath('_posts'),
|
||||
})
|
||||
|
||||
// eslint-disable-next-line array-callback-return
|
||||
files.map((f) => {
|
||||
const path = f.replace(/\.md$/, '')
|
||||
let markdown = fs.readFileSync(srcPath('_post', f), 'utf8')
|
||||
const makeHtml = new MakeHTML()
|
||||
|
||||
const [h, c = ''] = markdown.substr(4).split(/---\n(.*)$/s)
|
||||
const header = yaml.safeLoad(h, {
|
||||
schema: yaml.JSON_SCHEMA,
|
||||
}) as IMatter
|
||||
markdown = c
|
||||
await Promise.all(
|
||||
files.map(async (f) => {
|
||||
const path = f.replace(/\.md$/, '')
|
||||
const markdown = fs.readFileSync(srcPath('_posts', f), 'utf8')
|
||||
|
||||
const makeHtml = new MakeHTML(path)
|
||||
const [h] = markdown.substr(4).split(/---\n(.*)$/s)
|
||||
const header = yaml.safeLoad(h) as IMatter
|
||||
|
||||
const content = makeHtml.clean(markdown)
|
||||
const excerptHtml = makeHtml.parse(
|
||||
markdown.split(/<!-- excerpt(?:_separator)? -->/)[0]
|
||||
)
|
||||
const contentHtml = makeHtml.parse(markdown)
|
||||
const r = await makeHtml.parse(srcPath('_posts', f))
|
||||
const contentHtml = r.html()
|
||||
const excerptHtml = contentHtml.split(
|
||||
/<!-- excerpt(?:_separator)? -->/
|
||||
)[0]
|
||||
|
||||
db.insert({
|
||||
...header,
|
||||
path,
|
||||
content,
|
||||
excerptHtml,
|
||||
contentHtml,
|
||||
db.insert({
|
||||
...header,
|
||||
path,
|
||||
content: r.clean(),
|
||||
excerptHtml,
|
||||
contentHtml,
|
||||
})
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
|
||||
Reference in new issue
Block a user