防止HTML Tidy弄乱元标记(模式标记)

Joh*_*dam 6 html php wordpress html5 htmltidy

我正面临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>
Run Code Online (Sandbox Code Playgroud)

进入这些代码行 - 请仔细查看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>
Run Code Online (Sandbox Code Playgroud)

这导致了模式验证的一些严重问题.您可以在此处查看代码:https://search.google.com/structured-data/testing-tool/u/0/

由于此问题,客户端(URL:https://techswami.in)痕迹导航在搜索结果中不可见.

我在美化什么?

我的客户希望我让他/她的网站的源代码看起来"干净,可读和整洁".

所以我使用这些代码行来使它适合他/她.

注意:此代码100%完美地适用于以下WordPress设置.

  • Nginx与FastCGI Cache/MariaDB
  • 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');
Run Code Online (Sandbox Code Playgroud)

}

我需要你们帮忙什么?

你能否告诉我如何防止HTML Tidy弄乱META TAGS.我需要参数.

谢谢.

Joh*_*dam 2

首先,我衷心感谢所有试图帮助我的人。

我已经找到了解决方案,我的解决方案的唯一问题是它不能解决 HTML-Tidy 问题。

所以,现在我不再使用 HTML-Tody,而是使用这个: https: //github.com/ivanweiler/beautify-html/blob/master/beautify-html.php

我的新代码是:

if( !is_user_logged_in() || !is_admin() ) {
    function callback($buffer) {
        $html = $buffer;
        $beautify = new Beautify_Html(array(
          'indent_inner_html' => false,
          'indent_char' => " ",
          'indent_size' => 2,
          'wrap_line_length' => 32786,
          'unformatted' => ['code', 'pre'],
          'preserve_newlines' => false,
          'max_preserve_newlines' => 32786,
          'indent_scripts'  => 'normal' // keep|separate|normal
        ));

        $buffer = $beautify->beautify($html);
        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');
}
Run Code Online (Sandbox Code Playgroud)

现在,与架构标记相关的每个问题都已得到修复,并且客户站点的源代码也已美化。