Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow fallback to defaultRender in render method #444

Open
tafelnl opened this issue Jul 13, 2023 · 0 comments
Open

feat: allow fallback to defaultRender in render method #444

tafelnl opened this issue Jul 13, 2023 · 0 comments
Assignees
Labels

Comments

@tafelnl
Copy link

tafelnl commented Jul 13, 2023

We can supply linkifyStr with a custom render. Which is quite nice. But if we want to fallback to the default for some reason, that currently requires to copy-paste part of the source code.

For example:

import type { IntermediateRepresentation } from 'linkifyjs';
import linkifyStr from 'linkify-string';

/**
 * The functions `escapeText`, `escapeAttr`, `attributesToString`
 * and `defaultRender` are copied from the source code of linkifyjs.
 */

function escapeText(text: string) {
  return text
    .replace(/&/g, '&')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;');
}

function escapeAttr(href: string) {
  return href.replace(/"/g, '&quot;');
}

function attributesToString(
  attributes: IntermediateRepresentation['attributes']
) {
  const result = [];
  for (const attr in attributes) {
    const val = attributes[attr] + '';
    result.push(`${attr}="${escapeAttr(val)}"`);
  }
  return result.join(' ');
}

function defaultRender({
  tagName,
  attributes,
  content,
}: IntermediateRepresentation) {
  return `<${tagName} ${attributesToString(attributes)}>${escapeText(content)}</${tagName}>`;
}

export function example(value: string, className: string) {
  return linkifyStr(value, {
    className,
    render: {
      url: (options: IntermediateRepresentation) => {
        // some custom logic goes here
        // ...

        if (1 === 1) {
          // some more custom logic
          return '<i>Hello</i>';
        }

        return defaultRender(options);
      },
    },
  });
}

It would be nice to do something like this instead:

import { type IntermediateRepresentation, defaultRender } from 'linkifyjs';
import linkifyStr from 'linkify-string';

export function example(value: string, className: string) {
  return linkifyStr(value, {
    className,
    render: {
      url: (options: IntermediateRepresentation) => {
        // some custom logic goes here
        // ...

        if (1 === 1) {
          // some more custom logic
          return '<i>Hello</i>';
        }

        return defaultRender(options);
      },
    },
  });
}

Or maybe even:

import { type IntermediateRepresentation } from 'linkifyjs';
import linkifyStr from 'linkify-string';

export function example(value: string, className: string) {
  return linkifyStr(value, {
    className,
    render: {
      url: (options: IntermediateRepresentation) => {
        // some custom logic goes here
        // ...

        if (1 === 1) {
          // some more custom logic
          return '<i>Hello</i>';
        }

        return null;
      },
    },
  });
}
@nfrasser nfrasser self-assigned this Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants