|
| 1 | +/** |
| 2 | + * Giscus comment JSX component. |
| 3 | + * @module view/comment/giscus |
| 4 | + */ |
| 5 | +const { Component } = require('inferno'); |
| 6 | +const { cacheComponent } = require('../../util/cache'); |
| 7 | + |
| 8 | +/** |
| 9 | + * Giscus comment JSX component. |
| 10 | + * |
| 11 | + * @see https://giscus.app/ |
| 12 | + * @example |
| 13 | + * <Giscus |
| 14 | + * repo="usr/repo" |
| 15 | + * repoId="X_xxxxxxxxxx" |
| 16 | + * category="******" |
| 17 | + * categoryId="XXX_xxxxxxxxxxxxxxxx" |
| 18 | + * mapping="******" |
| 19 | + * strict={0} |
| 20 | + * reactionsEnabled={0} |
| 21 | + * emitMetadata={0} |
| 22 | + * inputPosition="******" |
| 23 | + * theme="******" |
| 24 | + * lang="******" |
| 25 | + * lazy="******" /> |
| 26 | + */ |
| 27 | +class Giscus extends Component { |
| 28 | + render() { |
| 29 | + const { repo, repoId, category, categoryId, mapping, term, strict, reactionsEnabled, emitMetadata, inputPosition, theme, customThemeCss, lang, lazy } = this.props; |
| 30 | + if (!repo || !repoId || !categoryId) { |
| 31 | + return ( |
| 32 | + <div class="notification is-danger"> |
| 33 | + You forgot to set the <code>repo</code>, <code>repoId</code>, or{' '} |
| 34 | + <code>categoryId</code> for Giscus. Please set it in <code>_config.yml</code>. |
| 35 | + </div> |
| 36 | + ); |
| 37 | + } |
| 38 | + if ((mapping == "specific" || mapping == "number") && !term) { |
| 39 | + return ( |
| 40 | + <div class="notification is-danger"> |
| 41 | + You set <code>mapping</code> to <code>specific</code> or <code>number</code>, but did not set <code>term</code> for Giscus. Please set <code>term</code> in <code>_config.yml</code>. |
| 42 | + </div> |
| 43 | + ); |
| 44 | + } |
| 45 | + const config = { repo }; |
| 46 | + if (category) { |
| 47 | + config['data-category'] = category; |
| 48 | + } else { |
| 49 | + config['data-category'] = 'Announcements'; |
| 50 | + } |
| 51 | + if(mapping) { |
| 52 | + config['data-mapping'] = mapping; |
| 53 | + } else { |
| 54 | + config['data-mapping'] = 'pathname'; |
| 55 | + } |
| 56 | + if(strict) { |
| 57 | + config['data-strict'] = 1; |
| 58 | + } else { |
| 59 | + config['data-strict'] = 0; |
| 60 | + } |
| 61 | + if(reactionsEnabled) { |
| 62 | + config['data-reactions-enabled'] = 1; |
| 63 | + } else { |
| 64 | + config['data-reactions-enabled'] = 0; |
| 65 | + } |
| 66 | + if(emitMetadata) { |
| 67 | + config['data-emit-metadata'] = 1; |
| 68 | + } else { |
| 69 | + config['data-emit-metadata'] = 0; |
| 70 | + } |
| 71 | + if(inputPosition) { |
| 72 | + config['data-input-position'] = inputPosition; |
| 73 | + } else { |
| 74 | + config['data-input-position'] = 'top' |
| 75 | + } |
| 76 | + if (theme) { |
| 77 | + if (theme == "custom") { |
| 78 | + if (customThemeCss) { |
| 79 | + config['data-theme'] = customThemeCss; |
| 80 | + } else { |
| 81 | + return ( |
| 82 | + <div class="notification is-danger"> |
| 83 | + You set <code>theme</code> to <code>custom</code>, but did not apply a <code>customThemeCss</code> for Giscus. Please set it in <code>_config.yml</code>. |
| 84 | + </div> |
| 85 | + ); |
| 86 | + } |
| 87 | + } else { |
| 88 | + config['data-theme'] = theme; |
| 89 | + } |
| 90 | + } else { |
| 91 | + config['data-theme'] = 'noborder_light'; |
| 92 | + } |
| 93 | + if (lang) { |
| 94 | + config['data-lang'] = lang; |
| 95 | + } else { |
| 96 | + config['data-lang'] = 'en'; |
| 97 | + } |
| 98 | + if (lazy) { |
| 99 | + config['data-loading'] = 'lazy'; |
| 100 | + } |
| 101 | + return ( |
| 102 | + <script |
| 103 | + src="https://giscus.app/client.js" |
| 104 | + {...config} |
| 105 | + crossorigin="anonymous" |
| 106 | + async={true}></script> |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Cacheable Giscus comment JSX component. |
| 113 | + * <p> |
| 114 | + * This class is supposed to be used in combination with the <code>locals</code> hexo filter |
| 115 | + * ({@link module:hexo/filter/locals}). |
| 116 | + * |
| 117 | + * @see module:util/cache.cacheComponent |
| 118 | + * @example |
| 119 | + * <Giscus.Cacheable |
| 120 | + * comment={{ |
| 121 | + * repo="******" |
| 122 | + * repo-id="******" |
| 123 | + * category="******" |
| 124 | + * category-id="******" |
| 125 | + * mapping="="******"" |
| 126 | + * strict="0" |
| 127 | + * reactions-enabled="0" |
| 128 | + * emit-metadata="0" |
| 129 | + * input-position="="******"" |
| 130 | + * theme="******" |
| 131 | + * lang="en" |
| 132 | + * loading="lazy" |
| 133 | + * }} /> |
| 134 | + */ |
| 135 | +Giscus.Cacheable = cacheComponent(Giscus, 'comment.giscus', (props) => { |
| 136 | + const { repo, repoId, category, categoryId, mapping, term, strict, reactionsEnabled, emitMetadata, inputPosition, theme, customThemeCss, lang, lazy } = props.comment; |
| 137 | + |
| 138 | + return { |
| 139 | + repo, |
| 140 | + repoId, |
| 141 | + category, |
| 142 | + categoryId, |
| 143 | + mapping, |
| 144 | + term, |
| 145 | + strict, |
| 146 | + reactionsEnabled, |
| 147 | + emitMetadata, |
| 148 | + inputPosition, |
| 149 | + theme, |
| 150 | + customThemeCss, |
| 151 | + lang, |
| 152 | + lazy |
| 153 | + }; |
| 154 | +}); |
| 155 | + |
| 156 | +module.exports = Giscus; |
0 commit comments