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 all 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": [
2,
{
ignore: ["^@theme-original"],
},
],
},
settings: {
"import/resolver": {
Expand Down
Binary file added website/config/og/basic/RobotoMono-Bold.ttf
Binary file not shown.
Binary file added website/config/og/basic/RobotoMono-Regular.ttf
Binary file not shown.
Binary file added website/config/og/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.
11 changes: 11 additions & 0 deletions website/config/og/basic/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"image": "preview.png",
"font": "RobotoMono-Bold.ttf",
"layout": [
{
"name": "title",
"top": 400,
"left": 210
}
]
}
12 changes: 12 additions & 0 deletions website/config/og/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"outputDir": "assets/og",
"textWidthLimit": 1100,
"quality": 70,
"rules": [
{
"name": "basic",
"priority": 0,
"pattern": "."
}
]
}
12 changes: 12 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 All @@ -10,6 +11,13 @@ const TWITTER = "https://twitter.com/feature_sliced";
const OPEN_COLLECTIVE = "https://opencollective.com/feature-sliced";
const DEFAULT_LOCALE = "ru";

const DOCUSAURUS_PLUGIN_OG = [
path.resolve(__dirname, "./plugins/docusaurus-plugin-og"),
{
templatesDir: path.resolve(__dirname, "config/og"),
},
];

/** @typedef {import('@docusaurus/types').DocusaurusConfig} Config */

/** @type {Config["themeConfig"]["navbar"]} */
Expand Down Expand Up @@ -254,6 +262,8 @@ const plugins = [
},
],
process.env.HOTJAR_ID && "docusaurus-plugin-hotjar", // For preventing crashing
// FIXME: Docusaurus Open Graph Plugin Experimental.
process.env.OG_EXP && DOCUSAURUS_PLUGIN_OG,
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
].filter(Boolean);

/** @type {Config["themeConfig"]["algolia"]} */
Expand Down Expand Up @@ -313,6 +323,8 @@ const metadatas = [
*/
const customFields = {
legacyRoutes: LEGACY_ROUTES,
// FIXME: Open Graph Experimental Mode.
isOGExperimental: process.env.OG_EXP,
};

/** @type {Config} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ const Config = object({
});

function validateConfig(config) {
if (is(config, Config)) {
return config.rules.reduce((validationResult, rule) => {
if (!is(rule, Rule)) return false;
return validationResult;
}, true);
}
return false;
return is(config, Config);
}

module.exports = { getConfig };
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ function createSVGText(
) {
const attributes = { fill, stroke };
const options = { fontSize, anchor: "top", attributes };
// Remove all emoji from text
const filteredText = text.replace(
/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g,
"",
);

/* If font width more than widthLimit => scale font width to ~90% of widthLimit */
if (widthLimit) {
const { width } = font.getMetrics(text, options);
if (width > widthLimit)
options.fontSize = Math.trunc((fontSize * 0.9) / (width / widthLimit));
}

return font.getSVG(text, options);
return font.getSVG(filteredText, options);
}
module.exports = { createSVGText, createFontsMapFromTemplates };
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { Logger } = require("./utils");

module.exports = function (_, { templatesDir }) {
return {
name: "docusaurus-plugin-open-graph-image",
name: "docusaurus-plugin-og",
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
async postBuild({ plugins, outDir, i18n }) {
Logger.info(`OG: work in progress.`);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { resolve } = require("path");
const { readdir, readFile } = require("fs/promises");
const { object, string, number, array, is } = require("superstruct");
const { object, string, number, array, is, optional } = require("superstruct");
const { objectFromBuffer, Logger } = require("./utils");

const dirIgnore = ["config.json"];
Expand Down Expand Up @@ -38,11 +38,11 @@ async function getTemplates(templatesDir, encode = "utf8") {

// TODO: May be with postEffects, images and etc? (optional fontSize, fill and etc)
const Layout = object({
type: string(),
type: optional(string()),
name: string(),
fontSize: number(),
fill: string(),
stroke: string(),
fontSize: optional(number()),
fill: optional(string()),
stroke: optional(string()),
top: number(),
left: number(),
});
Expand All @@ -54,16 +54,7 @@ const Template = object({
});

function validateTemplate({ params }) {
if (is(params, Template)) {
if (params.layout.length === 0) return false;

return params.layout.reduce((validationResult, layout) => {
if (!is(layout, Layout)) return false;
return validationResult;
}, true);
}

return false;
return is(params, Template);
}

module.exports = { getTemplates };
11 changes: 11 additions & 0 deletions website/src/shared/lib/og/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 OGMeta({ imgUrl }) {
return (
<Head>
<meta name="og:image" content={imgUrl} />
<meta name="twitter:image" content={imgUrl} />
</Head>
);
}
20 changes: 20 additions & 0 deletions website/src/theme/DocItem/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react";
import OriginalDocItem from "@theme-original/DocItem";
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
import { OGMeta } from "@site/src/shared/lib/og";
import { useDocOGUrl } from "./lib";

function DocItem(props) {
azinit marked this conversation as resolved.
Show resolved Hide resolved
const { content } = props;
const { isOGExperimental } = useDocusaurusContext().siteConfig.customFields;
const ogUrl = useDocOGUrl(content.metadata);

return (
<>
{isOGExperimental && <OGMeta imgUrl={ogUrl} />}
Krakazybik marked this conversation as resolved.
Show resolved Hide resolved
<OriginalDocItem {...props} />
</>
);
}

export default DocItem;
12 changes: 12 additions & 0 deletions website/src/theme/DocItem/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sha1 from "sha1";
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";

export function useDocOGUrl(metadata, buildOGPath = "assets/og/") {
const { siteConfig, i18n } = useDocusaurusContext();
const hashFileName = sha1(metadata.id + i18n.currentLocale);

/* OG Preview images build with locale prefix (.../en/assets/...) for not default locales */
return `${siteConfig.url}${
i18n.currentLocale !== i18n.defaultLocale ? `/${i18n.currentLocale}` : ""
}/${buildOGPath}${hashFileName}.jpg`;
}