restrictRead.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * 检查文章是否受限
  3. * @param post
  4. * @returns {boolean}
  5. */
  6. export function checkPostRestrictRead(post) {
  7. const annotations = post?.metadata?.annotations;
  8. const restrictReadEnable = annotations?.restrictReadEnable;
  9. if (restrictReadEnable === 'false') return false;
  10. const restrictType = restrictReadEnable;
  11. const raw = post.content.raw;
  12. const startTag = `<!-- ${restrictType}:restrict-read-html-tpl start -->`;
  13. const endTag = `<!-- ${restrictType}:restrict-read-html-tpl end -->`;
  14. // 使用正则模糊匹配(允许前后有空白字符)
  15. const startRegex = new RegExp(`\\s*${escapeRegExp(startTag)}\\s*`);
  16. const endRegex = new RegExp(`\\s*${escapeRegExp(endTag)}\\s*`);
  17. return startRegex.test(raw) && endRegex.test(raw);
  18. }
  19. /**
  20. * 替换受限内容
  21. * @param post
  22. * @param replacement
  23. * @returns {*}
  24. */
  25. export function replaceRestrictedContent(post, replacement = '') {
  26. const annotations = post?.metadata?.annotations;
  27. const restrictReadEnable = annotations?.restrictReadEnable;
  28. if (restrictReadEnable === 'false') return post.content.raw;
  29. const restrictType = restrictReadEnable;
  30. const raw = post.content.raw;
  31. const startTag = `<!-- ${restrictType}:restrict-read-html-tpl start -->`;
  32. const endTag = `<!-- ${restrictType}:restrict-read-html-tpl end -->`;
  33. const startRegex = new RegExp(`\\s*${escapeRegExp(startTag)}\\s*`, 'g');
  34. const endRegex = new RegExp(`\\s*${escapeRegExp(endTag)}\\s*`, 'g');
  35. // 构造完整匹配的正则
  36. const pattern = `${startRegex.source}(.*?)${endRegex.source}`;
  37. const regex = new RegExp(pattern, 'gs');
  38. return raw.replace(regex, replacement);
  39. }
  40. // 常量定义(可抽离到 constants.js)
  41. const PLACEHOLDER = 'restrict-read-placeholder';
  42. /**
  43. * 获取可展示的HTML内容块
  44. * @param {Object} post - 文章对象
  45. * @returns {string[]} - 分割后的HTML片段数组
  46. */
  47. export function getShowableContent(post) {
  48. const restrictEnabled = checkPostRestrictRead(post);
  49. const rawContent = post?.content?.raw || '';
  50. // 替换受限内容为占位符
  51. const processedContent = restrictEnabled
  52. ? replaceRestrictedContent(post, PLACEHOLDER)
  53. : rawContent;
  54. // 按占位符分割内容
  55. const contentFragments = processedContent
  56. .split(PLACEHOLDER)
  57. .map(fragment => fragment.trim())
  58. .filter(fragment => fragment);
  59. // 移除最后一个元素如果它只包含HTML标签而无实际文本
  60. if (contentFragments.length > 0 && isHtmlEmpty(contentFragments[contentFragments.length - 1])) {
  61. contentFragments.pop();
  62. }
  63. console.log('contentFragments:', contentFragments);
  64. return contentFragments;
  65. }
  66. /**
  67. * 获取受限阅读类型名称
  68. * @param post
  69. * @returns {string}
  70. */
  71. export function getRestrictReadTypeName(post) {
  72. const annotations = post?.metadata?.annotations;
  73. const restrictReadEnable = annotations?.restrictReadEnable;
  74. if (restrictReadEnable === 'false') return '';
  75. if (restrictReadEnable === 'password') return '密码';
  76. if (restrictReadEnable === 'code') return '验证码';
  77. if (restrictReadEnable === 'login') return '登录';
  78. if (restrictReadEnable === 'pay') return '付费';
  79. if (restrictReadEnable === 'comment') return '评论';
  80. }
  81. /**
  82. * 复制文本到剪贴板
  83. * @param text
  84. * @returns {Promise<void>}
  85. */
  86. export async function copyToClipboard(text) {
  87. try {
  88. await uni.setClipboardData({
  89. data: text,
  90. success: () => {
  91. uni.showToast({
  92. title: '复制成功',
  93. icon: 'success'
  94. });
  95. }
  96. });
  97. } catch (error) {
  98. console.error('复制出错:', error);
  99. }
  100. }
  101. /**
  102. * 转义字符串用于正则表达式
  103. * @param string
  104. * @returns {*}
  105. */
  106. function escapeRegExp(string) {
  107. return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  108. }
  109. /**
  110. * 判断字符串去除HTML标签后是否为空
  111. * @param {string} html
  112. * @returns {boolean}
  113. */
  114. function isHtmlEmpty(html) {
  115. return !html || !html.replace(/<[^>]+>/g, '').trim();
  116. }