import * as React from 'react';
import { Markdown } from '../../components/Markdown';
/**
 * Displays a file whose contents are rendered to HTML, such as a Markdown file.
 */
export class RenderedFile extends React.PureComponent {
    componentDidMount() {
        if (this.props.dangerousInnerHTML && this.props.location.hash) {
            this.scrollToHash(this.props.location.hash);
        }
    }
    componentDidUpdate(prevProps) {
        if (prevProps.dangerousInnerHTML !== this.props.dangerousInnerHTML ||
            prevProps.location.hash !== this.props.location.hash) {
            // Try scrolling when either the content or the hash changed.
            this.scrollToHash(this.props.location.hash);
        }
    }
    render() {
        return (React.createElement("div", { className: "rendered-file" },
            React.createElement("div", { className: "rendered-file__container" },
                React.createElement(Markdown, { dangerousInnerHTML: this.props.dangerousInnerHTML }))));
    }
    /** Scroll to the anchor in the page identified by the location fragment (e.g., #my-section). */
    scrollToHash(hash) {
        if (!hash) {
            return;
        }
        // This does not cause a page navigation (because window.location.hash === hash already), but it does cause
        // the page to scroll to the hash. This is simpler than using scrollTo, scrollIntoView, etc. Also assigning
        // window.location.hash does not trigger a navigation when `window.location.hash === hash`, so we can't
        // just use that.
        //
        // Finally, ensure that hash begins with a "#" so that this can't be used to redirect to arbitrary URLs (as
        // an open redirect vulnerability). This should always be true, but just be safe and document this
        // assertion in code.
        if (hash.startsWith('#')) {
            window.location.href = hash;
        }
    }
}
//# sourceMappingURL=RenderedFile.js.map