All files / js/components RecordComponent.tsx

50% Statements 6/12
25% Branches 2/8
25% Functions 1/4
50% Lines 5/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  1x         4x 1x   9x 3x                                                                                                      
import { useHistory } from "react-router";
import { urlToFsPath, fsToUrlPath } from "../utils";
 
export function getRecordPathAndAlt(
  path: string
): [string | null, string | null] {
  if (!path) {
    return [null, null];
  }
  const [p, a] = path.split(/\+/, 2);
  return [urlToFsPath(p), a];
}
 
/**
 * Helper to generate URL path for an admin page.
 * @param name - Name of the page (or null for the current one)
 * @param path - Record path
 */
export function pathToAdminPage(name: string, path: string) {
  return `${$LEKTOR_CONFIG.admin_root}/${path}/${name}`;
}
 
/** Details about the path to a Lektor record. */
interface RecordPathDetails {
  /** Path of the current record (or null). */
  path: string | null;
  /** The alternative of the record (or '_primary'). */
  alt: string;
}
 
/**
 * Extract a file system path and the alt from an URL path.
 * @param urlPath - A url path, i.e., a path with `:` as a separator and
 *                  potentially an alt appended with `+` at the end.
 */
export function getRecordDetails(urlPath: string): RecordPathDetails {
  const [path, alt] = getRecordPathAndAlt(urlPath);
  return {
    path,
    alt: !alt ? "_primary" : alt,
  };
}
 
export type RecordProps = {
  match: { params: { path: string; page: string } };
  record: RecordPathDetails;
  history: ReturnType<typeof useHistory>;
};
 
/**
 * Get the URL record path for a given record fs path.
 * @param path
 * @param alt
 */
export function getUrlRecordPathWithAlt(
  path: string | null,
  alt: string
): string {
  const urlPath = fsToUrlPath(path || "");
  return alt === "_primary" ? urlPath : `${urlPath}+${alt}`;
}