lodash#ReplaceFunction TypeScript Examples

The following examples show how to use lodash#ReplaceFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: documentation.ts    From ui5-language-assistant with Apache License 2.0 5 votes vote down vote up
function replace(
  string: string,
  matcher: RegExp,
  replacement: string | ReplaceFunction
): string {
  // @ts-expect-error - 'replace' is defined with 2 overloads instead of a union type in the definitions file
  return string.replace(matcher, replacement);
}
Example #2
Source File: documentation.ts    From ui5-language-assistant with Apache License 2.0 5 votes vote down vote up
tagMatcherToReplacement: {
  matcher: RegExp;
  replacement: Record<ConvertTarget, string | ReplaceFunction>;
}[] = [
  // Italics
  {
    matcher: /<i>(.+?)<\/i>/g,
    replacement: { markdown: "*$1*", plaintext: "$1" },
  },

  // Bold
  {
    matcher: /<b>(.+?)<\/b>/g,
    replacement: { markdown: "**$1**", plaintext: "$1" },
  },
  {
    matcher: /<strong>(.+?)<\/strong>/g,
    replacement: { markdown: "**$1**", plaintext: "$1" },
  },

  // Emphasis
  {
    matcher: /<em>(.+?)<\/em>/g,
    replacement: { markdown: "***$1***", plaintext: "$1" },
  },

  // Headers
  {
    matcher: /<h1>(.+?)<\/h1>/g,
    replacement: { markdown: "\n# $1\n\n", plaintext: "\n$1\n" },
  },
  {
    matcher: /<h2>(.+?)<\/h2>/g,
    replacement: { markdown: "\n## $1\n\n", plaintext: "\n$1\n" },
  },
  {
    matcher: /<h3>(.+?)<\/h3>/g,
    replacement: { markdown: "\n### $1\n\n", plaintext: "\n$1\n" },
  },
  {
    matcher: /<h4>(.+?)<\/h4>/g,
    replacement: { markdown: "\n#### $1\n\n", plaintext: "\n$1\n" },
  },

  // Lists
  {
    matcher: /<li>(.+?)<\/li>/g,
    replacement: { markdown: "\n* $1", plaintext: "\n* $1" },
  },
  { matcher: /<ul>/g, replacement: { markdown: "", plaintext: "" } },
  { matcher: /<\/ul>/g, replacement: { markdown: "\n\n", plaintext: "\n" } },
  // TODO should we handle ordered lists (ol)?

  // Code value
  {
    matcher: /<code>(.+?)<\/code>/g,
    replacement: { markdown: "`$1`", plaintext: "$1" },
  },

  // Code block
  {
    matcher: /<pre>([^]+?)<\/pre>/g,
    replacement: {
      markdown: "\n```javascript\n$1```\n",
      plaintext: "\n$1\n",
    },
  },

  // Line break
  { matcher: /<br\/>/g, replacement: { markdown: "\n", plaintext: "\n" } },
  { matcher: /<br>/g, replacement: { markdown: "\n", plaintext: "\n" } },
  { matcher: /<\/br>/g, replacement: { markdown: "\n", plaintext: "\n" } },

  // HTML Escaping, e.g. "&lt;View" --> "<View"
  // Note: this doesn't replace all html-encoded characters, only the most common ones
  {
    matcher: /.*/gm,
    replacement: { markdown: unescape, plaintext: unescape },
  },
]