Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PROMO-251: Connect OpenGraph plugin to Docusaurus [part2; split of PR-368] #375

Merged
merged 14 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions website/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ module.exports = {
},
],
"linebreak-style": [2, "unix"],
"import/no-unresolved": [
"error",
{
ignore: ["^@theme-original"],
},
],
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
},
settings: {
"import/resolver": {
Expand Down
7 changes: 7 additions & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require("dotenv").config();
const path = require("path");
const { REDIRECTS, SECTIONS, LEGACY_ROUTES } = require("./routes.config");

const DOMAIN = "https://feature-sliced.design/";
Expand Down Expand Up @@ -254,6 +255,12 @@ const plugins = [
},
],
process.env.HOTJAR_ID && "docusaurus-plugin-hotjar", // For preventing crashing
[
path.resolve(__dirname, "./plugins/docusaurus-plugin-open-graph-image"),
{
templatesDir: path.resolve(__dirname, "open-graph-templates"),
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
},
],
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
].filter(Boolean);

/** @type {Config["themeConfig"]["algolia"]} */
Expand Down
Binary file added website/open-graph-templates/basic/arial.ttf
Binary file not shown.
Binary file added website/open-graph-templates/basic/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions website/open-graph-templates/basic/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"image": "preview.png",
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
"font": "arial.ttf",
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
"layout": [
{
"type": "text",
"name": "title",
"fontSize": 80,
"fill": "white",
"stroke": "white",
"top": 400,
"left": 210
}
]
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
}
12 changes: 12 additions & 0 deletions website/open-graph-templates/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"outputDir": "assets/og",
azinit marked this conversation as resolved.
Show resolved Hide resolved
"textWidthLimit": 1100,
"quality": 70,
"rules": [
{
"name": "basic",
"priority": 0,
"pattern": "."
}
]
}
11 changes: 11 additions & 0 deletions website/src/shared/lib/open-graph-preview/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";
import Head from "@docusaurus/Head";

export function OpenGraphPreview({ imgUrl }) {
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
return (
<Head>
<meta name="og:image" content={imgUrl} />
<meta name="twitter:image" content={imgUrl} />
azinit marked this conversation as resolved.
Show resolved Hide resolved
</Head>
);
}
27 changes: 27 additions & 0 deletions website/src/theme/DocItem/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sha1 from "sha1";
import React from "react";
import OriginalDocItem from "@theme-original/DocItem";
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
import { OpenGraphPreview } from "@site/src/shared/lib/open-graph-preview";

function DocItem(props) {
azinit marked this conversation as resolved.
Show resolved Hide resolved
azinit marked this conversation as resolved.
Show resolved Hide resolved
const { content } = props;
const { id } = content.metadata;
const { siteConfig, i18n } = useDocusaurusContext();

const hashFileName = sha1(id + i18n.currentLocale);

return (
<>
<OpenGraphPreview
imgUrl={`${siteConfig.url}${
/* OG Preview images build with locale prefix (.../en/assets/...) for not default locales */
i18n.currentLocale !== i18n.defaultLocale ? `/${i18n.currentLocale}` : ""
}/assets/og/${hashFileName}.jpg`}
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
/>
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
<OriginalDocItem {...props} />
</>
);
}

export default DocItem;