HTML 链接降价有什么好的语法可以使内部链接更容易?

发布于 2024-08-07 04:59:10 字数 287 浏览 6 评论 0原文

我想创建一个好的语法,以便更容易链接到内部事物,有点像 Wiki,所以也许我认为用户可以使用类似的东西:

Bah bah bah reference: [[a]]

它会将其转换为 HTML:

Bah bah bah reference: <a href="..../id=a">title</a>

但我担心插入和安全问题。选择什么好的语法(可能是 [[]] 或其他东西)以及它的安全正则表达式是什么?

I want to create a good syntax for making it easier to link to internal things, kind of like a Wiki, so maybe I'm thinking the user can use something like:

Bah bah bah reference: [[a]]

And it would convert it into HTML:

Bah bah bah reference: <a href="..../id=a">title</a>

but I'm worry about interjection and security issues. What's a good syntax to pick (maybe [[]] or something else) and what's a safe regex for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

|煩躁 2024-08-14 04:59:10

不要重新发明轮子:Text::Markdown

Don't reinvent the wheel: Text::Markdown.

世态炎凉 2024-08-14 04:59:10

虽然 Sinan 的使用现有语法(例如 Markdown)的想法是一个很好的想法,但这种方法的可行性取决于您的情况的具体情况。您没有告诉我们足够多的信息,无法得到非常有帮助的答复。您是从零开始,还是继承了大量现有文件,而这些文件需要昂贵的转换过程,更不用说员工再培训?

如果是后者,有时最好通过考虑短期解决方法和长期策略来解决问题。短期解决方法——某种与你的问题中提出的完全一样的本土语法——会很丑陋,并且可能会到处引起麻烦,但它可以完成工作。例如,我们的组织有大量的 Word 文档,最终为 HTML 页面提供内容。在这些 Word 文件中,工作人员使用这样的方法来创建链接并做出一些由我们的解析代码处理的其他声明:##some_link##。导致这种情况失败的原因有很多,但在我们制作的内容类型中,这种情况很少发生。部分出于这个原因,很难对将内容迁移到更强大的系统的长期策略产生很大的热情。我的期望是这样的迁移将会发生,但它是由更大的考虑因素驱动的,而不是由我们笨拙的 ##foo## 标记设备的限制驱动的。

更新:

根据您的其他评论,听起来您将拥有一个链接列表,您希望用户能够使用短 ID 快速添加这些链接。因此,这些链接将在某个地方定义,比如 Perl 哈希。您可以使用此哈希来防止无效条目。例如:

use strict;
use warnings;

my %approved_links = (
    so_perl => 'http://stackoverflow.com/questions/tagged/perl',
    so_ruby => 'http://stackoverflow.com/questions/tagged/ruby',
);

my $regex = qr/ \[\[ ([^\]]+) \]\] /x;

while (<DATA>){
    die "Danger: $_\n" if map {
        exists $approved_links{$_} ? () : ($_)
    } /$regex/g;
    s/$regex/$approved_links{$1}/g; # Not complete, but you get the idea.
    print;
}

__DATA__
Some text [not an id] [[so_perl]] more [[so_ruby]] more..
[[so_perl]] blah [[so_ruby]] blah
[[bad id!!]]

While Sinan's idea of using an existing syntax, such as Markdown, is a good one, the viability of this sort of approach depends on the particulars of your situation. You haven't told us enough to get very helpful responses. Are you starting from a blank slate or are you inheriting a large body of existing documents that would require a costly conversion process, not to mention staff retraining?

If the latter, sometimes it's best to tackle the problem by thinking in terms of short-term workarounds and long-term strategies. The short-term workaround -- some sort of home-grown syntax precisely like that proposed in your question -- will be ugly and may cause hassles here and there, but it can get the job done. For example, our organization has a large pile of Word documents that ultimately provide content for HTML pages. Within those Word files, staff member use an approach like this to create links and make a few other declarations that our parsing code handles: ##some_link##. There are many ways for this to fail, but in the type of content that we are producing, it rarely occurs. Partly for that reason, it's difficult to generate much enthusiasm for a long-term strategy of migrating the content to a more robust system. My expectation is that such a migration will occur, but it is being driven by larger considerations and not by the limitations of our kludgy ##foo## markup device.

Update:

Based on your additional comments, it sounds like you will have a list of links that you want your users to be able to add quickly, using short IDs. So, those links will be defined someplace, say a Perl hash. You can use this hash to guard against invalid entries. For example:

use strict;
use warnings;

my %approved_links = (
    so_perl => 'http://stackoverflow.com/questions/tagged/perl',
    so_ruby => 'http://stackoverflow.com/questions/tagged/ruby',
);

my $regex = qr/ \[\[ ([^\]]+) \]\] /x;

while (<DATA>){
    die "Danger: $_\n" if map {
        exists $approved_links{$_} ? () : ($_)
    } /$regex/g;
    s/$regex/$approved_links{$1}/g; # Not complete, but you get the idea.
    print;
}

__DATA__
Some text [not an id] [[so_perl]] more [[so_ruby]] more..
[[so_perl]] blah [[so_ruby]] blah
[[bad id!!]]
栀子花开つ 2024-08-14 04:59:10

类似于 Textile克里奥尔语?为什么不使用现有的实现?

Something like Textile or Creole? Why don't you use an existing implementation?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文