2023-09-23 20:02:04 +02:00
|
|
|
import type { NavItem } from '@nuxt/content/dist/runtime/types';
|
2023-12-01 16:33:58 +01:00
|
|
|
import type { LocaleObject } from '@nuxtjs/i18n/dist/runtime/composables';
|
2023-09-26 14:57:26 +02:00
|
|
|
import { parseURL } from 'ufo';
|
2023-09-23 20:02:04 +02:00
|
|
|
|
2023-09-26 14:57:26 +02:00
|
|
|
/**
|
|
|
|
* オブジェクトのパス文字列からオブジェクトの内部を参照
|
|
|
|
* @param o オブジェクト
|
|
|
|
* @param s パス
|
|
|
|
* @returns パスの先にあるもの
|
|
|
|
*/
|
2023-07-09 20:09:50 +02:00
|
|
|
export function resolveObjPath(o: object, s: string): any {
|
|
|
|
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
|
|
|
|
s = s.replace(/^\./, ''); // strip a leading dot
|
|
|
|
var a = s.split('.');
|
|
|
|
for (var i = 0, n = a.length; i < n; ++i) {
|
|
|
|
var k = a[i];
|
|
|
|
if (k in o) {
|
|
|
|
o = o[k];
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return o;
|
2023-07-15 10:32:48 +02:00
|
|
|
}
|
|
|
|
|
2023-09-26 14:57:26 +02:00
|
|
|
/**
|
|
|
|
* URLがドメイン内部かどうかを判別
|
|
|
|
* @param link 判別したいURL
|
|
|
|
* @param base ローカルの基準となるドメイン
|
|
|
|
*/
|
2023-07-15 10:32:48 +02:00
|
|
|
export function isLocalPath(link: string, base?: string): boolean {
|
|
|
|
let baseUrl;
|
2023-09-26 14:57:26 +02:00
|
|
|
|
2023-07-15 10:32:48 +02:00
|
|
|
if (base) {
|
|
|
|
baseUrl = base;
|
|
|
|
} else {
|
|
|
|
const runtimeConfig = useRuntimeConfig();
|
|
|
|
baseUrl = runtimeConfig.public.baseUrl;
|
|
|
|
}
|
2023-09-26 14:57:26 +02:00
|
|
|
|
|
|
|
const rootDomain = parseURL(base);
|
|
|
|
const url = parseURL(link);
|
|
|
|
|
|
|
|
return (!url.host || rootDomain.host === url.host);
|
2023-07-18 11:14:54 +02:00
|
|
|
}
|
|
|
|
|
2023-11-25 17:48:16 +01:00
|
|
|
export function sanitizeInternalPath(path: string): string {
|
|
|
|
const runtimeConfig = useRuntimeConfig();
|
2023-12-01 16:33:58 +01:00
|
|
|
return path
|
|
|
|
.replace(/^(\/((?!ja)[a-z]{2}))?\/blog\/(.+)/g, '/ja/blog/$3')
|
|
|
|
.replace(new RegExp(`^(\/(${(runtimeConfig.public.locales as LocaleObject[]).map((l) => l.code).join('|')})\/?){2,}(.*)$`, 'g'), '$1$2');
|
2023-11-25 17:48:16 +01:00
|
|
|
}
|
|
|
|
|
2023-09-26 14:57:26 +02:00
|
|
|
/**
|
|
|
|
* ナビゲーションObjectを合致する条件まで深掘り
|
|
|
|
* @param obj ナビゲーションObject
|
|
|
|
* @param condition 深掘りを停止する条件
|
|
|
|
* @returns 深掘りしたナビゲーションObject
|
|
|
|
*/
|
2023-09-23 20:02:04 +02:00
|
|
|
export const findDeepObject = (obj: NavItem, condition: (v: NavItem) => boolean): NavItem | null => {
|
2023-07-18 11:14:54 +02:00
|
|
|
if (condition(obj)) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj?.children && obj.children.length > 0) {
|
|
|
|
for (let i = 0; i < obj.children.length; i++) {
|
|
|
|
const result = findDeepObject(obj.children[i], condition);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|