format-dialog-answer-custom-markdown-str.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { formatStaticAssetsUrl } from '../../utils/utils';
  2. /**
  3. * 格式化文档引用的数据
  4. * 将对象转换为字符串
  5. * @param {object} refference
  6. * @param {string} field_key - 数据中的相关字段
  7. */
  8. export function formatRefferenceString(refference, field_key) {
  9. if (!refference) {
  10. return '';
  11. }
  12. const doc_aggs = refference.doc_aggs;
  13. if (!doc_aggs) {
  14. return '';
  15. }
  16. // 仅返回一个容器,模板内容通过组件生成
  17. return `<div class="client-formated-markdown-element-item client-formated-markdown-doc-refference-container"></div>`;
  18. const refs_markdown = [];
  19. if (Array.isArray(doc_aggs)) {
  20. doc_aggs.forEach((item, index) => {
  21. refs_markdown.push(
  22. `<li
  23. class="client-formated-markdown-element-item client-formated-markdown-doc-refference-item"
  24. data-doc-id="${item.doc_id}"
  25. data-real-index="${index}"
  26. data-ref-key="${field_key}"
  27. >
  28. <span class="refference-index">${index + 1}</span>
  29. <span class="refference-label">${item.doc_name}</span>
  30. </li>`
  31. );
  32. });
  33. }
  34. const li_str = refs_markdown.join('');
  35. const ref_header = `<header class="client-formated-markdown-element-item client-formated-markdown-refference-header">
  36. <div>来源</div>
  37. <div>查看全部关联</div>
  38. </header>`;
  39. const ref_list = `<ul class="client-formated-markdown-element-item client-formated-markdown-doc-refference-container" data-ref-key="${field_key}">${li_str}</ul>`;
  40. return `${ref_header} ${ref_list}`;
  41. }
  42. export function formatRefferenceFlag(answer) {
  43. if (!answer) {
  44. return '';
  45. }
  46. let cur_text = answer;
  47. cur_text = cur_text.replace(/##\d\$\$/g, function (match, index, full) {
  48. // console.log(match, index, full);
  49. // match示例数据 ##1$$ ##2$$
  50. // 通过两次替换,字符串只剩下序号
  51. const match_v1 = match.replace('##', '');
  52. const match_v2 = match_v1.replace('$$', '');
  53. const ref_idx = match_v2;
  54. return `<span class="client-formated-markdown-element-item client-formated-markdown-doc-refference-flag" data-ref-index="${ref_idx}"></span>`;
  55. });
  56. return cur_text;
  57. }
  58. /**
  59. * 处理文件下载链接
  60. * @param {string} file_name
  61. * @param {string} file_url
  62. * @param {string} file_id
  63. */
  64. export function formatFileDownloadStr(file_name, file_url, file_id) {
  65. if (!file_name || !file_url) {
  66. return '';
  67. }
  68. let real_url = file_url.replace('//', '/');
  69. real_url = file_url.replace('/./', '/');
  70. real_url = file_url.replace('./', '/');
  71. real_url = formatStaticAssetsUrl(real_url);
  72. return `<div class="client-formated-markdown-element-item client-formated-download-container">
  73. <a class="download-link" href="${real_url}" target="_blank">
  74. <span class="download-icon">
  75. <svg viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" class="arco-icon arco-icon-download" stroke-width="4" stroke-linecap="butt" stroke-linejoin="miter" filter="downl" data-v-2bc6460e="">
  76. <path d="m33.072 22.071-9.07 9.071-9.072-9.07M24 5v26m16 4v6H8v-6"></path>
  77. </svg>
  78. </span>
  79. <span>${file_name}</span>
  80. </a>
  81. </div>`;
  82. }