Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add language control selector for static build #225

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/Controls/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const b = block('dc-controls');

export interface ControlsProps {
lang?: Lang;
langs?: string[];
langs?: Lang[];
fullScreen?: boolean;
singlePage?: boolean;
wideFormat?: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.dc-controls__lang-item {
padding: 0 18px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import {List, Popover} from '@gravity-ui/uikit';
import block from 'bem-cn-lite';
import allLangs from 'langs';

import {usePopupState, useTranslation} from '../../../hooks';
import {Lang} from '../../../models';
import {getPopupPosition} from '../../../utils';
import {Control} from '../../Control';
import {ControlsLayoutContext} from '../ControlsLayout';

import '../Controls.scss';
import {usePopupState, useTranslation} from '../../../../hooks';
import {Lang} from '../../../../models';
import {getPopupPosition} from '../../../../utils';
import {Control} from '../../../Control';
import {ControlsLayoutContext} from '../../ControlsLayout';

import '../../Controls.scss';
import './LangControl.scss';

const DEFAULT_LANGS = ['en', 'ru', 'he'];
const LEGACY_LANG_ITEMS = [
Expand Down
2 changes: 1 addition & 1 deletion src/components/Controls/single-controls/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export {default as FullScreenControl} from './FullScreenControl';
export {default as SettingsControl} from './SettingsControl/SettingsControl';
export {default as SinglePageControl} from './SinglePageControl';
export {default as LangControl} from './LangControl';
export {default as LangControl} from './LangControl/LangControl';
export {default as DividerControl} from './DividerControl/DividerControl';
export {default as PdfControl} from './PdfControl';
export {default as EditControl} from './EditControl';
2 changes: 1 addition & 1 deletion src/components/DocPage/DocPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const b = block('dc-doc-page');

export interface DocPageProps extends DocPageData, DocSettings {
lang: Lang;
langs?: string[];
langs?: Lang[];
router: Router;
headerHeight?: number;
tocTitleIcon?: React.ReactNode;
Expand Down
14 changes: 13 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type ChangeHandler<S> = <K extends keyof S, V extends S[K]>(
) => () => void;

export function normalizePath(path?: string | null) {
return path?.replace(/\/index$/, '/');
return path?.replace(/\.html$/, '')?.replace(/\/index$/, '/');
}

export function normalizeHash(hash?: string | null) {
Expand All @@ -45,6 +45,18 @@ export function isActiveItem(router: Router, href: string, singlePage?: boolean)
return normalizePath(router.pathname) === normalizePath(parse(href).pathname);
}

/*
makamekm marked this conversation as resolved.
Show resolved Hide resolved
Algorithm:
1. Normalize hash route if it presents
2. Split by "../" and take the last value
5. Join the result
*/
export function getLangPath(router: Router, lang: string) {
const path = router.hash ? normalizeHash(router.hash) : normalizePath(router.pathname);
const route = path?.split('../') || [];
return `../${lang}/${route[route.length - 1] || ''}`;
}

export function isExternalHref(href: string) {
return href.startsWith('http') || href.startsWith('//');
}
Expand Down
Loading