File size: 1,013 Bytes
57aa51e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import removeMd from 'remove-markdown';
import { makeId } from '../../../nestjs-libraries/src/services/make.is';

export const removeMarkdown = (params: { text: string; except?: RegExp[] }) => {
  let modifiedText = params.text;
  const except = params.except || [];
  const placeholders: { [key: string]: string } = {};

  // Step 2: Replace exceptions with placeholders
  except.forEach((regexp, index) => {
    modifiedText = modifiedText.replace(regexp, (match) => {
      const placeholder = `[[EXCEPT_PLACEHOLDER_${makeId(5)}]]`;
      placeholders[placeholder] = match;
      return placeholder;
    });
  });

  // Step 3: Remove markdown from modified text
  // Assuming removeMd is the function that removes markdown
  const cleanedText = removeMd(modifiedText);

  // Step 4: Replace placeholders with original text
  const finalText = Object.keys(placeholders).reduce((text, placeholder) => {
    return text.replace(placeholder, placeholders[placeholder]);
  }, cleanedText);

  return finalText;
};