-
-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathcontent-region.js
More file actions
80 lines (74 loc) · 1.89 KB
/
content-region.js
File metadata and controls
80 lines (74 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Markup from 'preact-markup';
import widgets from './widgets';
import style from './content-region.module.less';
import { useTranslation } from '../lib/i18n';
const COMPONENTS = {
...widgets,
pre: widgets.CodeBlock,
img(props) {
return <img decoding="async" {...props} />;
},
a(props) {
if (!props.target && props.href.match(/:\/\//)) {
props.target = '_blank';
props.rel = 'noopener noreferrer';
}
return <a {...props} />;
}
};
function SiblingNav({ route, lang, start }) {
let title = '';
let url = '';
if (route) {
url = route.path.toLowerCase();
title =
typeof route.name === 'object'
? route.name[lang || 'en']
: route.name || route.title;
}
const label = useTranslation(start ? 'previous' : 'next');
return (
<a class={style.nextLink} data-dir-end={!start} href={url}>
{start && <span class={style.icon}>← </span>}
{!start && <span class={style.icon}> →</span>}
<span class={style.nextInner}>
<span class={style.nextTitle}>
<span class={style.nextTitleInner}>{title}</span>
</span>
<span class={style.nextUrl}>{label}</span>
</span>
</a>
);
}
export default function ContentRegion({ content, components, ...props }) {
const hasNav = !!(props.next || props.prev);
components = Object.assign({}, COMPONENTS, components);
// TODO: Add props.name + '-rtl' class if lang is RTL
return (
<content-region name={props.name} data-page-nav={hasNav}>
{content && (
<Markup
// key={content}
markup={content}
type="html"
trim={false}
components={components}
/>
)}
{hasNav && (
<div class={style.nextWrapper}>
{props.prev ? (
<SiblingNav start lang={props.lang} route={props.prev} />
) : (
<span />
)}
{props.next ? (
<SiblingNav lang={props.lang} route={props.next} />
) : (
<span />
)}
</div>
)}
</content-region>
);
}