2
0

utils.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import type { SortOrder } from "antd/lib/table/interface";
  2. import lodash from "lodash";
  3. /**
  4. * 巴利三藏文本类型(chapter/para 等)路由位于 /workspace/tipitaka 下。
  5. */
  6. const TIPITAKA_ARTICLE_TYPES = new Set(["chapter", "para", "cs-para", "page"]);
  7. /**
  8. * 根据 article 类型和 id 生成 v6 前端路由路径。
  9. * - chapter / para → /workspace/tipitaka/:type/:id
  10. * - 其余已迁移类型(article / term / anthology / tag ...)→ /workspace/:type/:id
  11. */
  12. export function articlePath(type: string, id: string): string {
  13. return TIPITAKA_ARTICLE_TYPES.has(type)
  14. ? `/workspace/tipitaka/${type}/${id}`
  15. : `/workspace/${type}/${id}`;
  16. }
  17. export function dashboardBasePath(): string {
  18. if (import.meta.env.BASE_URL.includes("http")) {
  19. //for CDN
  20. return import.meta.env.BASE_URL;
  21. } else {
  22. return window.location.origin + import.meta.env.BASE_URL;
  23. }
  24. }
  25. export function fullUrl(url: string): string {
  26. // BASE_URL 通常以 "/" 结尾,而调用方传入的 url 有的带前导 "/" 有的不带,
  27. // 这里统一去掉重复的斜杠,避免拼出 ".../pcd-v2026//workspace/..." 这种地址
  28. const base = import.meta.env.BASE_URL.replace(/\/+$/, "");
  29. const path = url.replace(/^\/+/, "");
  30. const prefix = base.includes("http")
  31. ? base //for CDN
  32. : window.location.origin + base;
  33. return path ? `${prefix}/${path}` : `${prefix}/`;
  34. }
  35. export function PaliToEn(pali: string): string {
  36. let output: string = pali.toLowerCase();
  37. output = output.replaceAll("ā", "a");
  38. output = output.replaceAll("ī", "i");
  39. output = output.replaceAll("ū", "u");
  40. output = output.replaceAll("ḍ", "d");
  41. output = output.replaceAll("ṭ", "t");
  42. output = output.replaceAll("ḷ", "l");
  43. output = output.replaceAll("ṅ", "n");
  44. output = output.replaceAll("ṇ", "n");
  45. output = output.replaceAll("ñ", "n");
  46. output = output.replaceAll("ṃ", "m");
  47. return output;
  48. }
  49. export function PaliReal(inStr: string | undefined | null): string {
  50. if (typeof inStr !== "string") {
  51. return "";
  52. }
  53. const paliLetter = "abcdefghijklmnoprstuvyāīūṅñṭḍṇḷṃ";
  54. let output: string = "";
  55. inStr = inStr.toLowerCase();
  56. inStr = inStr.replace(/ṁ/g, "ṃ");
  57. inStr = inStr.replace(/ŋ/g, "ṃ");
  58. for (const iterator of inStr) {
  59. if (paliLetter.includes(iterator)) {
  60. output += iterator;
  61. }
  62. }
  63. return output;
  64. }
  65. export const getSorterUrl = (sorter?: Record<string, SortOrder>): string => {
  66. let url: string = "";
  67. for (const key in sorter) {
  68. if (Object.prototype.hasOwnProperty.call(sorter, key)) {
  69. const element = sorter[key];
  70. const dir = element === "ascend" ? "asc" : "desc";
  71. let orderby = key;
  72. if (orderby === "updatedAt") {
  73. orderby = "updated_at";
  74. }
  75. url = `&order=${orderby}&dir=${dir}`;
  76. }
  77. }
  78. return url;
  79. };
  80. export const convertToPlain = (html: string): string => {
  81. // Create a new div element
  82. const tempDivElement = document.createElement("div");
  83. // Set the HTML content with the given value
  84. tempDivElement.innerHTML = html;
  85. // Retrieve the text property of the element
  86. return tempDivElement.textContent || tempDivElement.innerText || "";
  87. };
  88. export const randomString = (): string => {
  89. return lodash.times(20, () => lodash.random(35).toString(36)).join("");
  90. };
  91. /**
  92. * 10进制数字转为16进制字符串
  93. * @param {number} arg
  94. * @returns
  95. */
  96. /*
  97. 作者:sq800
  98. 链接:https://juejin.cn/post/7250029395024281656
  99. 来源:稀土掘金
  100. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  101. */
  102. export const numToHex = (arg: number) => {
  103. try {
  104. const a = arg.toString(16).toUpperCase();
  105. return a.length % 2 === 1 ? "0" + a : a;
  106. } catch (e) {
  107. console.warn("数字转16进制出错:", e);
  108. }
  109. };
  110. export const scrollToTop = () => {
  111. document.getElementById("article-root")?.scrollIntoView();
  112. };