Giter Club home page Giter Club logo

linkify's Introduction

Linkify

A package to convert all links in a string to Markdown, HTML, or your own custom format.

Painlessly Convert:

'This text has a link https://github.com/taylornetwork/linkify and also another one https://google.com'

To:

'This text has a link [github.com](https://github.com/taylornetwork/linkify) and also another one [google.com](https://google.com)'
 
// OR 
 
'This text has a link <a href="https://github.com/taylornetwork/linkify">github.com</a> and also another one <a href="https://google.com">google.com</a>'

Install

Via Composer

$ composer require taylornetwork/linkify

Usage

See Config Options

Linkify Class

Basic Usage

use TaylorNetwork\Linkify\Linkify;

$text = 'This has a link. https://google.com';

$linkify = new Linkify;
$linkify->parse($text);

Returns

'This has a link. [google.com](https://google.com)'

Basic Usage - Static Call

use TaylorNetwork\Linkify\Linkify;

$text = 'This has a link. https://google.com';

Linkify::instance()->parse($text);

Returns

'This has a link. [google.com](https://google.com)'

Override Config

use TaylorNetwork\Linkify\Linkify;

$text = 'This has a link. https://google.com';

$linkify = new Linkify;
$linkify->setConfig('convertTo', Linkify::ConvertHTML);
$linkify->parse($text);

Returns

'This has a link. <a href="https://google.com">google.com</a>'

Override Config - Static, One Line

use TaylorNetwork\Linkify\Linkify;

$text = 'This has a link. https://google.com';

Linkify::instance()->setConfig('convertTo', Linkify::ConvertHTML)->parse($text);

Returns

'This has a link. <a href="https://google.com">google.com</a>'

Makes Links Trait

The MakesLinks trait will allow you to access the parser with a linkify($text) method on your class.

Basic Usage

use TaylorNetwork\Linkify\MakesLinks;

class DummyClass
{
	use MakesLinks;
	
	protected $text = 'This has a link. https://google.com';
	
	public function getParsedText()
	{
		return $this->linkify($this->text);
	}
}

getParsedText() returns

'This has a link. [google.com](https://google.com)'

Override Config

use TaylorNetwork\Linkify\MakesLinks;
use TaylorNetwork\Linkify\Linkify;

class DummyClass
{
	use MakesLinks;
	
	protected $text = 'This has a link. https://google.com';
	
	public function getParsedText()
	{
		return $this->linkify($this->text);
	}
	
	public function linkifyConfig(&$linkify)
	{
		$linkify->setConfig('convertTo', Linkify::ConvertHTML);
		$linkify->setConfig('linkAttributes', [
			'class' => 'btn-link'
		]);
	}
}

getParsedText() returns

'This has a link. <a class="btn-link" href="https://google.com">google.com</a>'

With Custom Format

use TaylorNetwork\Linkify\MakesLinks;
use TaylorNetwork\Linkify\Linkify;

class DummyClass
{
	use MakesLinks;
	
	protected $text = 'This has a link. https://google.com';
	
	public function getParsedText()
	{
		return $this->linkify($this->text);
	}
	
	public function linkifyConfig(&$linkify)
	{
		$linkify->setConfig('convertTo', Linkify::ConvertCustom);
	}
	
	public function linkifyCustomParse(string $caption, string $url) 
	{
		return '=>' . $caption . '<=#' . $url . '#';
	}
}

getParsedText() returns

'This has a link. =>google.com<=#https://google.com#'

Config

Publish the config using the artisan command.

$ php artisan vendor:publish --provider="TaylorNetwork\Linkify\LinkifyServiceProvider"

Will publish the config file to config/linkify.php


Link Format

You can change the default link format by changing the convertTo setting in config/linkify.php

'convertTo' => Linkify::ConvertMarkdown, // Converts to markdown links
// OR
'convertTo' => Linkify::ConvertHTML,     // Converts to <a> links
// OR
'convertTo' => Linkify::ConvertCustom,   // Your own custom callback

Link Attributes (HTML Only)

You can add any HTML link attributes you want to include in the <a> tag when generating a link (Don't use href).

Add the key and value to the linkAttributes array.

'linkAttributes' => [
	'target' => '_blank',
	'class' => 'btn-link',
],

Would generate links:

<a target="_blank" class="btn-link" href="$url">$caption</a>

Where $caption and $url would be replaced automatically.

Note: attributes are added in the order from the array and this only runs if using the Linkify::ConvertHTML setting in convertTo


Only format non-formatted links

The checkForExistingFormatting setting should be set to true if you want to only convert links that don't have existing formatting (default).

For example:

// Link is already formatted, by user, or another package, etc.
$text = 'Link: [link](https://google.com)';

Linkify::instance()->parse($text);

// Returns

// If 'checkForExistingFormatting' is true
'Link: [link](https://google.com)'

// If 'checkForExistingFormatting' is false
'Link: [link]([google.com](https://google.com))'

Custom Formatting

You can define a custom formatter in the config file, but prefer that you use the MakeLinks trait.

See Makes Links Trait

'customCallback' => function ($caption, $url) {
	return '{' . $caption . '}#' . $url . '#';
},

If 'convertTo' => Linkify::ConvertCustom the $caption and $url will be passed to the callback.

This would return: '{google.com}#https://google.com#'

License

MIT

linkify's People

Contributors

samueljtaylor avatar

Stargazers

 avatar

Watchers

 avatar  avatar

linkify's Issues

Possible problem when combining different formats

When checking for existing formatting we only check for whatever format we're converting to. This will pose a problem if pre formatted links are in a different format than we're converting to.

Example:

User provides:

Click [Here](http://url.test)

But we convert to HTML would likely return:

Click [Here](<a href="http://url.test">url.test</a>)

And vice versa

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.