-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·78 lines (71 loc) · 1.85 KB
/
Copy pathcli.js
File metadata and controls
executable file
·78 lines (71 loc) · 1.85 KB
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
#!/usr/bin/env node
const transformAndWriteToFile = require("json-to-frontmatter-markdown").default;
const axios = require("axios");
const { Observable, from, range } = require("rxjs");
const { concatMap, switchMap } = require("rxjs/operators");
const endpoint = "/endpoint" + "";
function createPost(post, path = "./src/posts") {
transformAndWriteToFile({
frontmatterMarkdown: {
frontmatter: [
{ title: post.title.rendered },
{ date: post.date.split(" ")[0] },
{
permalink: `article/${post.slug}/index.html`,
},
],
body: post.content.rendered,
},
path,
fileName: `${post.slug}.md`,
});
}
function copyPosts(endpoint, outdir) {
const posts = from(axios.get(endpoint))
.pipe(
switchMap(({ headers }) => range(1, Number(headers["x-wp-totalpages"])))
)
.pipe(
concatMap((page) =>
axios.get(endpoint, {
params: {
page,
},
})
)
);
const posts$ = posts.subscribe(
({ data }) => {
Object.keys(data).forEach((p, i) => {
const post = data[p];
createPost(post, outdir);
});
},
(err) => console.error("Oh no, an error!", err),
() => console.log("completed copy")
);
return posts$;
}
require("yargs")
.scriptName("wordpress-to-docusaurus")
.usage("$0 <cmd> <args>")
.command(
"posts [endpoint] [outdir]",
"copy Wordpress posts to Docusaurus directory",
(yargs) => {
yargs.positional("endpoint", {
describe: "Wordpress blog post endpoint",
nargs: 1,
demand: true,
demand: "endpoint is required",
});
yargs.positional("outdir", {
describe: "docusaurus blog output directory",
default: ".",
});
},
function (argv) {
copyPosts(argv.endpoint, argv.outdir);
}
)
.help().argv;