Skip to content

Commit 867d610

Browse files
committed
feat: init version of wordpress-to-docusaurus-plugin
0 parents  commit 867d610

8 files changed

Lines changed: 1220 additions & 0 deletions

File tree

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.DS_Store
2+
.vscode
3+
.idea
4+
*.iml
5+
*.code-workspace
6+
.changelog
7+
8+
node_modules
9+
10+
.eslintcache
11+
12+
yarn-error.log
13+
dist
14+
coverage
15+
.docusaurus
16+
.cache-loader
17+
types
18+
lib

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 rurumimic
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# wordpress-to-docusaurus-plugin
2+
3+
A Docusaurus Plugin which will import content from Wordpress based on a GraphQL query.
4+
5+
## Configuration
6+
7+
`yarn add wordpress-to-docusaurus-plugin`
8+
9+
or
10+
11+
`npm install --save wordpress-to-docusaurus-plugin`
12+
13+
Add this plugin to the plugins array in `docusaurus.config.js`.
14+
15+
```
16+
module.exports = {
17+
// ...
18+
plugins: [
19+
[require.resolve('wordpress-to-docusaurus-plugin'),
20+
{
21+
// URL of the Wordpress GraphQL endpoint
22+
url: 'http://my-wordpress-instance/graphql',
23+
// Output sub-directory, below site dir (default: '/blog')
24+
outputPath: '/blog',
25+
}
26+
]
27+
]
28+
}
29+
```
30+
31+
## Usage
32+
33+
When your site is built, the GraphQL endpoint for your configured `url` will be
34+
used to import posts to the path specified by `outputPath`.
35+
36+
From your Docusaurus project root directory
37+
38+
`yarn start`
39+
40+
or
41+
42+
`yarn build`
43+
44+

package.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "wordpress-to-docusaurus-plugin",
3+
"version": "0.0.1",
4+
"description": "Docusaurus plugin for importing Wordpress posts",
5+
"main": "dist/index.js",
6+
"repository": "https://github.com/mark-tate/wordpress-to-docusaurus-plugin",
7+
"author": "Mark Tate",
8+
"license": "MIT",
9+
"scripts": {
10+
"build": "tsc",
11+
"lint": "prettier --check src/**/*.ts",
12+
"format": "prettier --write src/**/*.ts"
13+
},
14+
"husky": {
15+
"hooks": {
16+
"pre-commit": "lint-staged"
17+
}
18+
},
19+
"lint-staged": {
20+
"{src}/**/*.{ts}": [
21+
"prettier --write"
22+
]
23+
},
24+
"dependencies": {
25+
"graphql": "^15.4.0",
26+
"graphql-request": "^3.4.0"
27+
},
28+
"engines": {
29+
"node": ">=10.9.0"
30+
},
31+
"devDependencies": {
32+
"@docusaurus/types": "^2.0.0-alpha.56",
33+
"@types/node": "^13.11.1",
34+
"@types/webpack-merge": "^4.1.5",
35+
"husky": "^4.3.6",
36+
"lint-staged": "^10.5.3",
37+
"prettier": "^2.2.1",
38+
"typescript": "^3.8.3"
39+
},
40+
"keywords": [
41+
"docusaurus",
42+
"wordpress",
43+
"markdown",
44+
"plugin"
45+
]
46+
}

src/index.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import fs from "fs";
2+
import path from "path";
3+
4+
import { request as graphQLRequest } from "graphql-request";
5+
import { PluginOptions, Post } from "./types";
6+
import { LoadContext } from "@docusaurus/types";
7+
8+
const query = `
9+
query {
10+
posts {
11+
edges {
12+
node {
13+
id
14+
slug
15+
title
16+
date
17+
excerpt
18+
featuredImage {
19+
node {
20+
sourceUrl
21+
}
22+
}
23+
content
24+
}
25+
}
26+
}
27+
}
28+
`;
29+
30+
function formatBlogDate(date: Date): String {
31+
var yyyy = date.getFullYear().toString();
32+
var mm = (date.getMonth() + 1).toString();
33+
var dd = date.getDate().toString();
34+
35+
var mmChars = mm.split("");
36+
var ddChars = dd.split("");
37+
38+
return (
39+
yyyy +
40+
"-" +
41+
(mmChars[1] ? mm : "0" + mmChars[0]) +
42+
"-" +
43+
(ddChars[1] ? dd : "0" + ddChars[0])
44+
);
45+
}
46+
47+
export default function pluginWordpressToDocusaurus(
48+
context: LoadContext,
49+
options: PluginOptions
50+
) {
51+
const { outputPath = "blog", url } = options;
52+
return {
53+
name: "wordpress-docusaurus-blog-plugin",
54+
async loadContent() {
55+
const response = await graphQLRequest(url, query);
56+
const { edges } = response.posts;
57+
edges.forEach((post: Post) => {
58+
const {
59+
content,
60+
date: dateStr,
61+
excerpt,
62+
featuredImage,
63+
slug,
64+
title,
65+
} = post.node;
66+
const blogDate = new Date(dateStr);
67+
const blogFile = `${formatBlogDate(blogDate)}-${slug}.md`;
68+
const blogPath = path.resolve(context.siteDir, outputPath, blogFile);
69+
const blogContent = content
70+
.replace(/<br>/g, " \n")
71+
.replace(/<\/li>/g, "</li> \n")
72+
.replace(/<pre class="wp-block-code"><code>/g, "\n```\n")
73+
.replace(/<\/code><\/pre>/g, "\n```\n")
74+
.replace(/<p>/g, "")
75+
.replace(/<\/p>/g, " `\n");
76+
const blogDoc = [
77+
"---",
78+
`slug: ${slug}`,
79+
`title: ${title}`,
80+
"---",
81+
excerpt,
82+
featuredImage
83+
? `<img src="${featuredImage.node.sourceUrl}" />\n`
84+
: null,
85+
"<!--truncate-->",
86+
blogContent,
87+
].join("\n");
88+
fs.writeFile(blogPath, blogDoc, function (err) {
89+
if (err) throw err;
90+
console.log(`Created Blog ${blogPath}`);
91+
});
92+
});
93+
},
94+
};
95+
}

src/types.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export interface PluginOptions {
2+
/** URL of the Wordpress GraphQL endpoint **/
3+
url: string;
4+
/** Output sub-directory, below site dir (default blog) */
5+
outputPath?: string;
6+
}
7+
8+
export interface Image {
9+
/** URL of image */
10+
node: {
11+
sourceUrl: string;
12+
};
13+
}
14+
15+
export interface Post {
16+
node: {
17+
/** Content of post */
18+
content: string;
19+
/** Date of post */
20+
date: string;
21+
/** Post excerpt, used by Blog index */
22+
excerpt: string;
23+
/** Featured image */
24+
featuredImage?: Image;
25+
/** Post id */
26+
slug: string;
27+
/** Post title description */
28+
title: string;
29+
};
30+
}

tsconfig.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2017",
4+
"module": "commonjs",
5+
"lib": [
6+
"es2017",
7+
"es2019.array"
8+
],
9+
"declaration": true,
10+
"declarationMap": true,
11+
/* Strict Type-Checking Options */
12+
"strict": true,
13+
"strictNullChecks": true,
14+
"strictFunctionTypes": true,
15+
"strictBindCallApply": true,
16+
"strictPropertyInitialization": true,
17+
"noImplicitThis": true,
18+
"alwaysStrict": true,
19+
/* Additional Checks */
20+
"noUnusedLocals": true,
21+
"noUnusedParameters": true,
22+
"noImplicitReturns": true,
23+
"noFallthroughCasesInSwitch": true,
24+
/* Module Resolution Options */
25+
"moduleResolution": "node",
26+
"allowSyntheticDefaultImports": true,
27+
"esModuleInterop": true,
28+
/* Advanced Options */
29+
"resolveJsonModule": true,
30+
/* Plugin Options */
31+
"incremental": true,
32+
"tsBuildInfoFile": "./dist/.tsbuildinfo",
33+
"rootDir": "src",
34+
"outDir": "dist"
35+
},
36+
"exclude": [
37+
"node_modules",
38+
"**/__tests__/**/*",
39+
"**/dist/**/*"
40+
]
41+
}

0 commit comments

Comments
 (0)