-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0c4450c
wip: define html tag definition
endiliey 0b12988
feat(v2): implement injectHtmlTags api
endiliey 40240a0
remove useless file
endiliey 0743f26
nits
endiliey 9c34531
documentation
endiliey bc49b28
Merge branch 'master' into endi/assettags
endiliey bb521fa
allow string, use prebodyTags and postBodyTags
endiliey d96d572
typo
endiliey e8db0c0
refactor typing
endiliey 21b2bee
chore(v2): bump deps
endiliey 3559a11
Merge branch 'endi/deps' into endi/assettags
endiliey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,13 @@ | |
<meta name="viewport" content="width=device-width"> | ||
<meta name="generator" content="Docusaurus"> | ||
<title><%= htmlWebpackPlugin.options.title %></title> | ||
<%= htmlWebpackPlugin.options.headTags %> | ||
<%= htmlWebpackPlugin.tags.headTags %> | ||
</head> | ||
<body> | ||
<%= htmlWebpackPlugin.options.preBodyTags %> | ||
<div id="__docusaurus"></div> | ||
<%= htmlWebpackPlugin.tags.bodyTags %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This too. |
||
<%= htmlWebpackPlugin.options.postBodyTags %> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/docusaurus/src/server/html-tags/__tests__/__fixtures__/plugin-empty.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; | ||
}; |
26 changes: 26 additions & 0 deletions
26
packages/docusaurus/src/server/html-tags/__tests__/__fixtures__/plugin-headTags.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* 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', | ||
}, | ||
}, | ||
`<meta name="generator" content="docusaurus">`, | ||
], | ||
}; | ||
}, | ||
}; | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/docusaurus/src/server/html-tags/__tests__/__fixtures__/plugin-postBodyTags.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* 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-postBody-tags', | ||
injectHtmlTags() { | ||
return { | ||
postBodyTags: [ | ||
{ | ||
tagName: 'div', | ||
innerHTML: 'Test content', | ||
}, | ||
], | ||
}; | ||
}, | ||
}; | ||
}; |
23 changes: 23 additions & 0 deletions
23
packages/docusaurus/src/server/html-tags/__tests__/__fixtures__/plugin-preBodyTags.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-preBodyTags', | ||
injectHtmlTags() { | ||
return { | ||
preBodyTags: { | ||
tagName: 'script', | ||
attributes: { | ||
type: 'text/javascript', | ||
}, | ||
innerHTML: 'window.foo = null;', | ||
}, | ||
}; | ||
}, | ||
}; | ||
}; |
122 changes: 122 additions & 0 deletions
122
packages/docusaurus/src/server/html-tags/__tests__/htmlTags.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"`, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this leftover by mistake?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no its not. So html webpack plugin by default will inject the required script at postbody. So since we want to add our own postbody tags, we have to set “inject:false” to htmlwebpackplugin and then put the injection place. See https://github.com/jantimon/html-webpack-plugin/tree/master/examples/custom-insertion-position
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*at posthead and postbody.