【发布时间】:2019-01-27 09:54:58
【问题描述】:
我在使用 HTML Tidy(最新版本 -- https://html-tidy.org)时遇到了一个严重问题。
简而言之:HTML 整洁地转换这些 HTML 代码行
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem">
<a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
<span property="name">Codes</span>
</a>
<meta property="position" content="1">
</span>
</div>
进入这些代码行——请仔细查看 META TAGS 位置。
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem">
<a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
<span property="name">Codes</span>
</a>
</span>
<meta property="position" content="1">
</div>
这会导致架构验证出现一些严重问题。你可以在这里查看代码:https://search.google.com/structured-data/testing-tool/u/0/
由于这个问题,客户端(URL:https://techswami.in)的面包屑导航在搜索结果中不可见。
我在美化什么?
我的客户希望我让他/她的网站源代码看起来“干净、可读和整洁”。
所以我正在使用这些代码行使其对他/她有用。
注意:此代码在以下 WordPress 设置中 100% 完美运行。
- 带有 FastCGI 缓存/MariaDB 的 Nginx
- PHP7
- Ubuntu 18.04.1
- 最新的 WordPress,兼容所有缓存插件。
代码:
if( !is_user_logged_in() || !is_admin() ) {
function callback($buffer) {
$tidy = new Tidy();
$options = array('indent' => true, 'markup' => true, 'indent-spaces' => 2, 'tab-size' => 8, 'wrap' => 180, 'wrap-sections' => true, 'output-html' => true, 'hide-comments' => true, 'tidy-mark' => false);
$tidy->parseString("$buffer", $options);
$tidy->cleanRepair();
$buffer = $tidy;
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { if (ob_get_length()) ob_end_flush(); }
add_action('wp_loaded', 'buffer_start');
add_action('shutdown', 'buffer_end');
}
我需要你们提供什么帮助?
您能告诉我如何防止 HTML Tidy 弄乱 META TAGS。我需要参数。
谢谢。
【问题讨论】:
-
你尝试过另一种 html tidy 方法吗?查看 github 上的 tidy-html5 ...有一个与您在此处描述的问题非常相似的问题,该问题已针对此应用程序解决 - github.com/htacg/tidy-html5/issues/333
-
告诉您的客户,他们的网站不可能由不相互通信的动态组件组成,因此每个组件都不知道他们需要如何更改自己的输出格式。您能做的最好的事情就是确保您创建的 PHP 代码干净整洁。然后通知您未受过教育的客户,查看源输出不是网站的源代码,而是为 Web 浏览器生成的代码。
-
@MartinBarker 我认为您应该再次阅读我的问题,我是说,我能够美化代码,我只是面临 标签中的 标签的单个问题。谈到您的第二点,当您查看源代码时,它实际上是“当前”网页应用程序的代码。我知道它是为网络浏览器生成的,甚至我的客户也知道。感谢您的不太有用的评论。
-
我确实读过它,我的总体观点是停止试图弄乱生成的源代码,因为虽然验证器正确报告它,因为它们是实验性的,所以不被信任,那个元标记是无效的w3schools.com/tags/tag_meta.asp 属性在元标记或全局属性列表中无效,元不应出现在头部之外,因此您的客户不仅要求您无法阅读您使用的标准...
标签: php wordpress html htmltidy