whitecube/laravel-string-typography

一组简单的 Str & Stringable 宏,帮助实现排版规则

v1.0.1 2024-07-26 12:56 UTC

This package is auto-updated.

Last update: 2024-08-26 13:11:30 UTC


README

用户提供的字符串渲染可能会导致意外的排版结果。例如,在大多数拉丁语(如法语)中,建议在感叹号或问号(!?)等符号前添加非断行空格。大多数用户可能会直接输入普通空格,这可能会导致在这些标点符号之前出现不想要的换行。

此简单包为 Laravel 的 Str 门面和 Stringable 实例(使用 Str::of()str() 创建)提供了一个 typography 宏,它会处理这些排版细节。

您还可以通过添加自己的排版规则来增强此包。

$content = 'Mama mia !';

echo str($content)->typography();
Mama mia !

安装

composer require whitecube/laravel-string-typography

入门

包的 ServiceProvider 和因此其 typography 宏将在安装时自动注册。您可以直接开始使用它

{{-- Using the "Str" facade --}}
<h1>{!! Str::typography($title) !!}</h1>

{{-- Using the "str()" helper --}}
<p>{!! str($paragraph)->typography() !!}</p>

使用 Stringable 实例,您可以链接 typography 与其他辅助方法

<div>{!! str($text)->markdown()->typography() !!}</div>

注意

由于转换后的字符串包含 HTML 实体(如 &nbsp;),请记住在您的 Blade 模板中使用 {!! !!} 而不是 {{ }} 来渲染它们。

默认排版规则

注册和删除排版规则

use Whitecube\Strings\Typography;

Typography::rule(
    key: 'a-to-b',
    regex: '/a/',
    callback: fn(array $matches) => 'b',
);

默认情况下,所有之前注册的排版规则都将应用于使用新 typography 方法的场景。您可以使用以下方式全局删除其中之一:

use Whitecube\Strings\Typography;

Typography::remove('a-to-b');

过滤排版规则

调用单个或几个特定规则

<div>{!! str($text)->typography(only: 'hellip') !!}</div>
<div>{!! str($text)->typography(only: ['hellip', 'non-breaking-punctuation']) !!}</div>

调用除单个或几个特定不想要的规则之外的所有规则

<div>{!! str($text)->typography(except: 'hellip') !!}</div>
<div>{!! str($text)->typography(except: ['hellip', 'non-breaking-punctuation']) !!}</div>

🔥 赞助

如果您在生产应用程序中依赖于此包,请考虑赞助我们!这是帮助我们继续做我们喜欢做的事情:制作出色的开源软件的最佳方式。

贡献

请随意提出更改建议、请求新功能或自行修复错误。我们确信还有许多可以改进的地方,并且我们非常乐意合并有用的拉取请求。谢谢!

用 ❤️ 为开源制作

Whitecube,我们每天都在日常工作中使用大量的开源软件。因此,当我们有机会回馈时,我们感到非常兴奋!

我们希望您会喜欢我们的小贡献,如果您在项目中发现它有用,我们非常愿意 听到您的反馈。在 Twitter 上关注我们以获取更多更新!