Skip to content

Commit

Permalink
refactor: combine DocMeta with Meta (#382)
Browse files Browse the repository at this point in the history
* refactor: rename DocMeta component to DocPageMeta

Renaming due to name collision with interface DocMeta from models

* feat: re-export DocPageMeta, SocialSharingMeta, Meta components

* feat: delete re-export for DocPageMeta component

* refactor: combine DocPageMeta with Meta component

Add DocPageMeta content in Meta.
Delete DocPageMeta.
  • Loading branch information
Pavel-Tyan authored Mar 3, 2025
1 parent a4f3c7a commit e9a716c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 48 deletions.
42 changes: 0 additions & 42 deletions src/components/DocMeta/DocMeta.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/DocMeta/index.ts

This file was deleted.

35 changes: 31 additions & 4 deletions src/components/Meta/Meta.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import {compose} from 'react-recompose';
import {Helmet} from 'react-helmet';

import {MetaProps, SocialSharingMeta} from '../SocialSharingMeta';
import {DocMeta, DocMetaProps} from '../DocMeta';
import withRouter, {WithRouterProps} from '../../hoc/withRouter';
import withLang, {WithLangProps} from '../../hoc/withLang';
import {sanitizeHtml} from '../../utils/sanitize';
Expand All @@ -12,7 +12,7 @@ interface SharingProps {
description?: string;
}

export interface MetaComponentProps extends DocMetaProps {
export interface MetaComponentProps {
type?: string;
url?: string;
image?: string;
Expand All @@ -27,6 +27,9 @@ export interface MetaComponentProps extends DocMetaProps {
schemaJsonLd?: unknown;
isAwsServer?: boolean;
metadata?: Record<string, string>[];
title?: string;
defaultTitle?: string;
titleTemplate?: string;
}

type MetaComponentInnerProps = MetaComponentProps & WithRouterProps & WithLangProps;
Expand Down Expand Up @@ -55,6 +58,31 @@ const Meta: React.FC<MetaComponentInnerProps> = (props) => {
return result;
};

const sanitizeObject = (target: Record<string, string>) => {
const clear = Object.entries(target).map(
([name, content]) => [sanitizeHtml(name), sanitizeHtml(content)] as [string, string],
);

return Object.fromEntries(clear);
};

const renderDocPageMeta = () => {
const title = sanitizeHtml(props.title);
const titleTemplate = sanitizeHtml(props.titleTemplate);
const defaultTitle = sanitizeHtml(props.defaultTitle);

const {metadata = []} = props;

return (
<Helmet title={title} defaultTitle={defaultTitle} titleTemplate={titleTemplate}>
{metadata.map((element, index) => {
/* list is immutable */
return <meta key={index} {...sanitizeObject(element)} />;
})}
</Helmet>
);
};

const {
extra,
router,
Expand All @@ -64,7 +92,6 @@ const Meta: React.FC<MetaComponentInnerProps> = (props) => {
noIndex,
canonical,
alternate,
...documentMetaProps
} = props;

const {
Expand All @@ -84,7 +111,7 @@ const Meta: React.FC<MetaComponentInnerProps> = (props) => {

return (
<React.Fragment>
<DocMeta {...documentMetaProps} />
{renderDocPageMeta()}
<SocialSharingMeta
type={type}
url={url || fullUrl}
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ export * from './components/DocPage';
export * from './components/DocPageTitle';
export * from './components/HTML';
export * from './components/Mark';
export * from './components/Meta';
export * from './components/MiniToc';
export * from './components/SubNavigation';
export * from './components/ShareButton';
export * from './components/SocialSharingMeta';
export * from './components/Scrollspy';
export * from './components/StageLabel';
export * from './components/Text';
Expand Down

0 comments on commit e9a716c

Please sign in to comment.