scrivito
Version:
Scrivito is a professional, yet easy to use SaaS Enterprise Content Management Service, built for digital agencies and medium to large businesses. It is completely maintenance-free, cost-effective, and has unprecedented performance and security.
56 lines (47 loc) • 1.6 kB
text/typescript
import { basicUrlForObj } from 'scrivito_sdk/app_support/basic_url_for';
import { currentAppSpace } from 'scrivito_sdk/app_support/current_app_space';
import { generateUrl } from 'scrivito_sdk/app_support/routing';
import { throwInvalidArgumentsError } from 'scrivito_sdk/common';
import { DataStack, getDataContextQuery } from 'scrivito_sdk/data_integration';
import { InternalUrl, formatInternalLinks } from 'scrivito_sdk/link_resolution';
import { getObjFrom } from 'scrivito_sdk/models';
/** @public */
export function resolveHtmlUrls(htmlString: string): string {
checkResolveHtmlUrls(htmlString);
return replaceInternalLinks(htmlString);
}
interface Options {
preserveObjId?: true;
dataStack?: DataStack;
}
export function replaceInternalLinks(
htmlString: string,
options?: Options
): string {
return formatInternalLinks(htmlString, (url) =>
calculateInternalLinkUrl(url, options)
);
}
function calculateInternalLinkUrl(
{ obj_id: objId, query, hash }: InternalUrl,
options?: Options
) {
const obj = getObjFrom(currentAppSpace(), objId);
if (!obj) return generateUrl({ objId, query, hash });
const dataStack = options?.dataStack;
return basicUrlForObj(obj, {
query: dataStack ? getDataContextQuery(obj, dataStack, query) : query,
hash,
...options,
withoutOriginIfLocal: true,
});
}
function checkResolveHtmlUrls(htmlString: string) {
if (typeof htmlString !== 'string') {
throwInvalidArgumentsError(
'resolveHtmlUrls',
"'htmlString' must be a 'String'.",
{ docPermalink: 'js-sdk/resolveHtmlUrls' }
);
}
}