UNPKG

@superset-ui/core

Version:
90 lines (75 loc) 4.11 kB
import _isArray from "lodash/isArray";import _mergeWith from "lodash/mergeWith";(function () {var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;enterModule && enterModule(module);})();var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {return a;}; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import { useEffect, useMemo, useState } from 'react'; import rehypeSanitize, { defaultSchema } from 'rehype-sanitize'; import remarkGfm from 'remark-gfm'; import { FeatureFlag, isFeatureEnabled } from '../utils';import { jsx as _jsx } from "@emotion/react/jsx-runtime"; export function getOverrideHtmlSchema( originalSchema, htmlSchemaOverrides) { return _mergeWith(originalSchema, htmlSchemaOverrides, (objValue, srcValue) => _isArray(objValue) ? objValue.concat(srcValue) : undefined ); } function SafeMarkdown({ source, htmlSanitization = true, htmlSchemaOverrides = {} }) { const escapeHtml = isFeatureEnabled(FeatureFlag.EscapeMarkdownHtml); const [rehypeRawPlugin, setRehypeRawPlugin] = useState(null); const [ReactMarkdown, setReactMarkdown] = useState(null); useEffect(() => { Promise.all([import('rehype-raw'), import('react-markdown')]).then( ([rehypeRaw, ReactMarkdown]) => { setRehypeRawPlugin(() => rehypeRaw.default); setReactMarkdown(() => ReactMarkdown.default); } ); }, []); const rehypePlugins = useMemo(() => { const rehypePlugins = []; if (!escapeHtml && rehypeRawPlugin) { rehypePlugins.push(rehypeRawPlugin); if (htmlSanitization) { const schema = getOverrideHtmlSchema( defaultSchema, htmlSchemaOverrides ); rehypePlugins.push([rehypeSanitize, schema]); } } return rehypePlugins; }, [escapeHtml, htmlSanitization, htmlSchemaOverrides, rehypeRawPlugin]); if (!ReactMarkdown || !rehypeRawPlugin) { return null; } // React Markdown escapes HTML by default return ( _jsx(ReactMarkdown, { rehypePlugins: rehypePlugins, remarkPlugins: [remarkGfm], skipHtml: false, transformLinkUri: null, children: source } )); }__signature__(SafeMarkdown, "useState{[rehypeRawPlugin, setRehypeRawPlugin](null)}\nuseState{[ReactMarkdown, setReactMarkdown](null)}\nuseEffect{}\nuseMemo{rehypePlugins}");const _default = SafeMarkdown;export default _default;;(function () {var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;if (!reactHotLoader) {return;}reactHotLoader.register(getOverrideHtmlSchema, "getOverrideHtmlSchema", "/Users/evan_1/GitHub/superset/superset-frontend/packages/superset-ui-core/src/components/SafeMarkdown.tsx");reactHotLoader.register(SafeMarkdown, "SafeMarkdown", "/Users/evan_1/GitHub/superset/superset-frontend/packages/superset-ui-core/src/components/SafeMarkdown.tsx");reactHotLoader.register(_default, "default", "/Users/evan_1/GitHub/superset/superset-frontend/packages/superset-ui-core/src/components/SafeMarkdown.tsx");})();;(function () {var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;leaveModule && leaveModule(module);})(); //# sourceMappingURL=SafeMarkdown.js.map