「Cms/mediawiki/h1タイトルをパンクズ化」の版間の差分

提供: 初心者エンジニアの簡易メモ
ナビゲーションに移動 検索に移動
編集の要約なし
編集の要約なし
 
(同じ利用者による、間の2版が非表示)
11行目: 11行目:
resources/custom-title.js
resources/custom-title.js
<pre>
<pre>
"resources/custom-title.js" 20L, 470B
// パンくずリンク
// パンくずリンク
document.addEventListener("DOMContentLoaded", function () {
document.addEventListener("DOMContentLoaded", function () {
21行目: 20行目:
     const html = parts.map(part => {
     const html = parts.map(part => {
         path.push(part);
         path.push(part);
         const encoded = path.join("/");
         const uri = path.join("/");
         return `<a href="/index.php/${encoded}">${part}</a>`;
         return `<a href="/index.php?title=${encodeURIComponent(uri)}">${part}</a>`;
     });
     });
     h1.innerHTML = html.join("/");
     h1.innerHTML = html.join("/");

2026年4月29日 (水) 17:34時点における最新版

バージョン1.45の場合

LocalSettings.php

$wgHooks['BeforePageDisplay'][] = function ( $out, $skin ) {

    $out->addScriptFile( '/resources/custom-title.js' );

    return true;
};

resources/custom-title.js

// パンくずリンク
document.addEventListener("DOMContentLoaded", function () {
    const h1 = document.querySelector("#firstHeading");
    if (!h1) return;
    const text = h1.textContent;
    const parts = text.split("/");
    let path = [];
    const html = parts.map(part => {
        path.push(part);
        const uri = path.join("/");
        return `<a href="/index.php?title=${encodeURIComponent(uri)}">${part}</a>`;
    });
    h1.innerHTML = html.join("/");
});

バージョン1.24の場合

スラッシュ(/)で区切られたページごとにリンクを貼ります。

  • skins/Vector/VectorTemplate.php

変更前

?>"><span dir="auto"><?php $this->html( 'title' ) ?></span></h1>

変更後

<span dir="auto">
<?php if ($_GET['action']): ?>
<?php $this->html( 'title' ) ?>
<?php else: ?>
<?php $name = $this->get( 'title' ); $names = explode('/', $name); $path = ""; ?>
<?php foreach ($names as $key => $name): ?><?php $path .= $name; ?><a href="/index.php/<?php echo htmlspecialchars($path); ?>"><?php echo htmlspecialchars($name); ?></a><?php if ($key + 1 < count($names)): ?>/<?php endif; ?><?php $path .= "/"; ?><?php endforeach; ?>
<?php endif; ?>
</span></h1>