]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/html-tags.tsx
5d532589efb62fe2a7c08f2f61ccfa0131a1c468
[lemmy-ui.git] / src / shared / components / common / html-tags.tsx
1 import { httpExternalPath } from "@utils/env";
2 import { htmlToText } from "html-to-text";
3 import { Component } from "inferno";
4 import { Helmet } from "inferno-helmet";
5 import { md } from "../../markdown";
6 import { I18NextService } from "../../services";
7
8 interface HtmlTagsProps {
9   title: string;
10   path: string;
11   description?: string;
12   image?: string;
13 }
14
15 /// Taken from https://metatags.io/
16 export class HtmlTags extends Component<HtmlTagsProps, any> {
17   render() {
18     const url = httpExternalPath(this.props.path);
19     const desc = this.props.description;
20     const image = this.props.image;
21
22     return (
23       <Helmet title={this.props.title}>
24         <html lang={I18NextService.i18n.resolvedLanguage} />
25
26         {["title", "og:title", "twitter:title"].map(t => (
27           <meta key={t} property={t} content={this.props.title} />
28         ))}
29         {["og:url", "twitter:url"].map(u => (
30           <meta key={u} property={u} content={url} />
31         ))}
32
33         {/* Open Graph / Facebook */}
34         <meta property="og:type" content="website" />
35
36         {/* Twitter */}
37         <meta property="twitter:card" content="summary_large_image" />
38
39         {/* Optional desc and images */}
40         {["description", "og:description", "twitter:description"].map(
41           n =>
42             desc && (
43               <meta
44                 key={n}
45                 name={n}
46                 content={htmlToText(md.renderInline(desc))}
47               />
48             )
49         )}
50         {["og:image", "twitter:image"].map(
51           p => image && <meta key={p} property={p} content={image} />
52         )}
53       </Helmet>
54     );
55   }
56 }