From 61e594786c8b6dfba2756f66a122645858696cc0 Mon Sep 17 00:00:00 2001 From: Misha Vyrtsev Date: Mon, 13 May 2019 18:51:17 +0300 Subject: [PATCH] Define remark host in runtime (#325) * convert closest-polyfill to ts * make remark respect host property in client setup --- README.md | 54 +++++++++++++++++-------- frontend/app/common/closest-polyfill.js | 15 ------- frontend/app/common/closest-polyfill.ts | 17 ++++++++ frontend/app/common/config-types.ts | 3 ++ frontend/app/common/constants.ts | 4 +- frontend/app/embed.ts | 8 ++-- frontend/app/utils/parseQuery.ts | 22 ++++++++++ frontend/comments.ejs | 17 ++++---- frontend/counter.ejs | 17 ++++---- frontend/deleteme.html | 5 +++ frontend/iframe.html | 9 +++++ frontend/index.ejs | 22 +++++----- frontend/last-comments.ejs | 15 ++++--- 13 files changed, 138 insertions(+), 70 deletions(-) delete mode 100644 frontend/app/common/closest-polyfill.js create mode 100644 frontend/app/common/closest-polyfill.ts create mode 100644 frontend/app/utils/parseQuery.ts diff --git a/README.md b/README.md index 378a68e1d8..237edd299a 100644 --- a/README.md +++ b/README.md @@ -308,18 +308,28 @@ Add this snippet to the bottom of web page: ```html ``` @@ -348,19 +358,24 @@ window.REMARK42.changeTheme('light'); It's a widget which renders list of last comments from your site. -Add this snippet to the bottom of web page: +Add this snippet to the bottom of web page, or adjust already present `remark_config` to have `last-comments` in `components` list: ```html ``` @@ -376,19 +391,24 @@ And then add this node in the place where you want to see last comments widget: It's a widget which renders a number of comments for the specified page. -Add this snippet to the bottom of web page: +Add this snippet to the bottom of web page, or adjust already present `remark_config` to have `counter` in `components` list: ```html ``` diff --git a/frontend/app/common/closest-polyfill.js b/frontend/app/common/closest-polyfill.js deleted file mode 100644 index d83819b246..0000000000 --- a/frontend/app/common/closest-polyfill.js +++ /dev/null @@ -1,15 +0,0 @@ -if (!Element.prototype.matches) { - Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; -} - -if (!Element.prototype.closest) { - Element.prototype.closest = function(s) { - let el = this; - - do { - if (el.matches(s)) return el; - el = el.parentElement || el.parentNode; - } while (el !== null && el.nodeType === 1); - return null; - }; -} diff --git a/frontend/app/common/closest-polyfill.ts b/frontend/app/common/closest-polyfill.ts new file mode 100644 index 0000000000..1fe57b046f --- /dev/null +++ b/frontend/app/common/closest-polyfill.ts @@ -0,0 +1,17 @@ +if (!Element.prototype.matches) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + Element.prototype.matches = (Element.prototype as any).msMatchesSelector || Element.prototype.webkitMatchesSelector; +} + +if (!Element.prototype.closest) { + Element.prototype.closest = function(s: string) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let el: any = this; + + do { + if (el.matches(s)) return el; + el = el.parentElement || el.parentNode; + } while (el !== null && el.nodeType === 1); + return null; + }; +} diff --git a/frontend/app/common/config-types.ts b/frontend/app/common/config-types.ts index df727d3ac5..369e5db2d3 100644 --- a/frontend/app/common/config-types.ts +++ b/frontend/app/common/config-types.ts @@ -1,6 +1,7 @@ import { UserInfo, Theme } from './types'; export interface CounterConfig { + host: string; site_id: string; url?: string; } @@ -8,6 +9,7 @@ export interface CounterConfig { export type UserInfoConfig = UserInfo; export interface CommentsConfig { + host: string; site_id: string; url?: string; max_shown_comments?: number; @@ -16,6 +18,7 @@ export interface CommentsConfig { } export interface LastCommentsConfig { + host: string; site_id: string; max_last_comments: number; } diff --git a/frontend/app/common/constants.ts b/frontend/app/common/constants.ts index 014f59cb31..e95482f75e 100644 --- a/frontend/app/common/constants.ts +++ b/frontend/app/common/constants.ts @@ -1,6 +1,8 @@ import { Sorting, AuthProvider, BlockingDuration, Theme } from './types'; -export const BASE_URL: string = process.env.REMARK_URL!; +export const BASE_URL: string = + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ((window as any).remark_config && (window as any).remark_config.host) || process.env.REMARK_URL!; export const API_BASE = '/api/v1'; export const NODE_ID: string = process.env.REMARK_NODE!; export const COUNTER_NODE_CLASSNAME = 'remark42__counter'; diff --git a/frontend/app/embed.ts b/frontend/app/embed.ts index 538dc267b4..f86e6397b8 100644 --- a/frontend/app/embed.ts +++ b/frontend/app/embed.ts @@ -7,6 +7,8 @@ import { BASE_URL, NODE_ID, COMMENT_NODE_CLASSNAME_PREFIX } from '@app/common/co import { UserInfo, Theme } from '@app/common/types'; import { CommentsConfig } from '@app/common/config-types'; +const HOST = remark_config.host || BASE_URL; + if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { @@ -14,7 +16,7 @@ if (document.readyState === 'loading') { } async function init(): Promise { - __webpack_public_path__ = BASE_URL + '/web/'; + __webpack_public_path__ = HOST + '/web/'; await loadPolyfills(); const node = document.getElementById(NODE_ID); @@ -47,7 +49,7 @@ async function init(): Promise { node.innerHTML = `