-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuxtify.js
103 lines (97 loc) · 2.75 KB
/
nuxtify.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
// vim: set ts=2 sts=2 sw=2 et :
let cheerio = require("cheerio")
let fs = require("fs-extra")
let path = require("path")
let src = path.join(__dirname, "output/localhost:5000")
let dst = path.join(__dirname, "output/vdex-web")
function replaceExt(filePath, ext) {
return path.join(path.dirname(filePath),
path.basename(filePath, path.extname(filePath)) + ext)
}
function breadcrumbs(relPath) {
let to = "/"
let b = [{
disabled: false,
text: "VDex",
to: to,
}]
let woExt = replaceExt(relPath, "")
if (path.basename(woExt) == "index") {
woExt = path.dirname(woExt)
}
for (let part of woExt.split(path.sep)) {
to += part + "/"
b.push({
disabled: false,
nuxt: true,
text: part,
to: to,
})
}
b[b.length - 1]["disabled"] = true
return b
}
function convert(relPath, content) {
let $ = cheerio.load(content)
let title = $("title").text().replace("|", "-")
let styles = $("style")
$("a").each(function(index, elem) {
let text = $(this).text()
let href = $(this).attr("href").replace("/", path.sep)
let woExt = replaceExt(href, "")
if (path.basename(woExt) == "index") {
woExt = path.dirname(woExt)
}
let joined = path.join(path.dirname(relPath), woExt)
let to = "/" + joined.replace(path.sep, "/")
let link = $("<a></a>")
link.attr("href", to)
link.text(text)
$(this).replaceWith(link)
})
let elements = $("body").children().slice(1)
let s = "<template>\n<div>\n"
s += "<div>\n"
for (let crumb of breadcrumbs(relPath)) {
if (crumb.disabled) {
let a = $("<span></span>")
a.text(crumb.text)
s += $.html(a) + "\n"
} else {
let a = $("<a></a>")
a.attr("href", crumb.to)
a.text(crumb.text)
s += $.html(a) + " /\n"
}
}
s += "</div>\n"
elements.each(function(index, elem) {
s += $.html($(this))
})
s += "</div>\n</template>\n<script lang=\"ts\">\n"
s += "import { Component, Vue } from \"nuxt-property-decorator\"\n"
s += "@Component\nexport default class Page extends Vue {\n"
s += "head() {\nreturn {\ntitle: " + JSON.stringify(title) + "\n} } "
s += "}\n</script>\n<style>\n"
styles.each(function(index, elem) {
s += $(this).html() + "\n"
})
s += "\n</style>\n"
return s
}
function walk(dir) {
for (let name of fs.readdirSync(dir, "utf8")) {
let entry = path.join(dir, name)
if (path.extname(entry) == ".html") {
let relPath = path.relative(src, entry)
let contents = fs.readFileSync(entry, "utf8")
let dstPath = path.join(dst, replaceExt(relPath, ".vue"))
let dstContents = convert(relPath, contents)
fs.ensureDirSync(path.dirname(dstPath))
fs.writeFileSync(dstPath, dstContents, "utf8")
} else {
walk(entry)
}
}
}
walk(src)