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

feat(v2): injectHtmlTags API to inject head and/or body html tags #2057

Merged
merged 11 commits into from
Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
29 changes: 28 additions & 1 deletion packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ export interface LoadContext {
baseUrl: string;
}

export interface Props extends LoadContext {
export interface HtmlTags {
headTags: string;
bodyTags: string;
}

export interface Props extends LoadContext, HtmlTags {
routesPaths: string[];
plugins: Plugin<any>[];
}
Expand Down Expand Up @@ -104,6 +109,10 @@ export interface Plugin<T> {
getPathsToWatch?(): string[];
getClientModules?(): string[];
extendCli?(cli: Command): void;
injectHtmlTags?(): {
headTags?: HtmlTagObject | HtmlTagObject[];
bodyTags?: HtmlTagObject | HtmlTagObject[];
};
}

export type PluginConfig = [string, Object] | [string] | string;
Expand Down Expand Up @@ -152,3 +161,21 @@ export interface ConfigureWebpackUtils {
getCacheLoader: (isServer: boolean, cacheOptions?: {}) => Loader | null;
getBabelLoader: (isServer: boolean, babelOptions?: {}) => Loader;
}

interface HtmlTagObject {
/**
* Attributes of the html tag
* E.g. `{'disabled': true, 'value': 'demo', 'rel': 'preconnect'}`
*/
attributes?: {
[attributeName: string]: string | boolean;
};
/**
* The tag name e.g. `div`, `script`, `link`, `meta`
*/
tagName: string;
/**
* The inner HTML
*/
innerHTML?: string;
}
1 change: 1 addition & 0 deletions packages/docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"express": "^4.17.1",
"fs-extra": "^8.1.0",
"globby": "^10.0.1",
"html-tags": "^3.1.0",
"html-webpack-plugin": "^4.0.0-beta.11",
"import-fresh": "^3.2.1",
"lodash": "^4.17.15",
Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus/src/client/serverEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import ssrTemplate from './templates/ssr.html.template';

// Renderer for static-site-generator-webpack-plugin (async rendering via promises)
export default async function render(locals) {
const {routesLocation} = locals;
const {routesLocation, headTags, bodyTags} = locals;
const location = routesLocation[locals.path];
await preload(routes, location);
const modules = new Set();
Expand Down Expand Up @@ -76,6 +76,8 @@ export default async function render(locals) {
chunkManifestScript,
htmlAttributes: htmlAttributes || '',
bodyAttributes: bodyAttributes || '',
headTags,
bodyTags,
metaAttributes,
scripts,
stylesheets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
<meta name="viewport" content="width=device-width">
<meta name="generator" content="Docusaurus">
<title><%= htmlWebpackPlugin.options.title %></title>
<%= htmlWebpackPlugin.options.headTags %>
</head>
<body>
<%= htmlWebpackPlugin.options.bodyTags %>
endiliey marked this conversation as resolved.
Show resolved Hide resolved
<div id="__docusaurus"></div>
</body>
</html>
2 changes: 2 additions & 0 deletions packages/docusaurus/src/client/templates/ssr.html.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = `
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="generator" content="Docusaurus">
<%- headTags %>
endiliey marked this conversation as resolved.
Show resolved Hide resolved
<%- chunkManifestScript %>
<% metaAttributes.forEach((metaAttribute) => { %>
<%- metaAttribute %>
Expand All @@ -21,6 +22,7 @@ module.exports = `
<% }); %>
</head>
<body <%- bodyAttributes %>>
<%- bodyTags %>
<div id="__docusaurus">
<%- appHtml %>
</div>
Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function start(
const protocol: string = process.env.HTTPS === 'true' ? 'https' : 'http';
const port: number = await getPort(cliOptions.port);
const host: string = getHost(cliOptions.host);
const {baseUrl} = props;
const {baseUrl, headTags, bodyTags} = props;
const urls = prepareUrls(protocol, host, port);
const openUrl = normalizeUrl([urls.localUrlForBrowser, baseUrl]);

Expand All @@ -92,6 +92,8 @@ export async function start(
),
filename: 'index.html',
title: siteConfig.title,
headTags,
bodyTags,
}),
// This is necessary to emit hot updates for webpack-dev-server
new HotModuleReplacementPlugin(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = function() {
return {
name: 'plugin-bodyTags-only',
injectHtmlTags() {
return {
bodyTags: {
tagName: 'script',
attributes: {
type: 'text/javascript',
},
innerHTML: 'window.foo = null;',
},
};
},
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = function() {
return {
name: 'plugin-both-tags',
injectHtmlTags() {
return {
headTags: [
endiliey marked this conversation as resolved.
Show resolved Hide resolved
{
tagName: 'link',
attributes: {
rel: 'preconnect',
href: 'www.google-analytics.com',
},
},
],
bodyTags: [
{
tagName: 'div',
innerHTML: 'Test content',
},
],
};
},
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = function() {
return {
name: 'plugin-empty',
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = function() {
return {
name: 'plugin-headTags-only',
injectHtmlTags() {
return {
headTags: [
{
tagName: 'link',
attributes: {
rel: 'preconnect',
href: 'www.google-analytics.com',
},
},
],
};
},
};
};
122 changes: 122 additions & 0 deletions packages/docusaurus/src/server/html-tags/__tests__/htmlTags.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {htmlTagObjectToString} from '../htmlTags';

describe('htmlTagObjectToString', () => {
test('simple html tag', () => {
expect(
htmlTagObjectToString({
tagName: 'script',
attributes: {
type: 'text/javascript',
src:
'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js',
async: true,
},
}),
).toMatchInlineSnapshot(
`"<script type=\\"text/javascript\\" src=\\"https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js\\" async></script>"`,
);

expect(
htmlTagObjectToString({
tagName: 'link',
attributes: {
rel: 'preconnect',
href: 'www.google-analytics.com',
},
}),
).toMatchInlineSnapshot(
`"<link rel=\\"preconnect\\" href=\\"www.google-analytics.com\\">"`,
);

expect(
htmlTagObjectToString({
tagName: 'div',
attributes: {
style: 'background-color:lightblue',
},
innerHTML: 'Lightblue color here',
}),
).toMatchInlineSnapshot(
`"<div style=\\"background-color:lightblue\\">Lightblue color here</div>"`,
);

expect(
htmlTagObjectToString({
tagName: 'div',
innerHTML: 'Test',
}),
).toMatchInlineSnapshot(`"<div>Test</div>"`);
});

test('valid html void tag', () => {
expect(
htmlTagObjectToString({
tagName: 'meta',
attributes: {
name: 'generator',
content: 'Docusaurus',
},
}),
).toMatchInlineSnapshot(
`"<meta name=\\"generator\\" content=\\"Docusaurus\\">"`,
);

expect(
htmlTagObjectToString({
tagName: 'img',
attributes: {
src: '/img/docusaurus.png',
alt: 'Docusaurus logo',
height: '42',
width: '42',
},
}),
).toMatchInlineSnapshot(
`"<img src=\\"/img/docusaurus.png\\" alt=\\"Docusaurus logo\\" height=\\"42\\" width=\\"42\\">"`,
);
});

test('invalid tag', () => {
expect(() =>
htmlTagObjectToString({
tagName: 'endiliey',
attributes: {
this: 'is invalid',
},
}),
).toThrowErrorMatchingInlineSnapshot(
`"Error loading {\\"tagName\\":\\"endiliey\\",\\"attributes\\":{\\"this\\":\\"is invalid\\"}}, \\"endiliey\\" is not a valid HTML tags"`,
);
});

test('invalid tagName', () => {
expect(() =>
htmlTagObjectToString({
tagName: true,
}),
).toThrowErrorMatchingInlineSnapshot(
`"{\\"tagName\\":true} is not a valid HTML tag object. \\"tagName\\" must be defined as a string"`,
);
});

test('invalid html tag object', () => {
expect(() =>
htmlTagObjectToString('fooofofoofo'),
).toThrowErrorMatchingInlineSnapshot(
`"\\"fooofofoofo\\" is not a valid HTML tag object"`,
);

expect(() =>
htmlTagObjectToString(null),
).toThrowErrorMatchingInlineSnapshot(
`"\\"null\\" is not a valid HTML tag object"`,
);
});
});
Loading