默认将文档分类下各子类别的着陆页重定向至索引文件

我也希望有一个内置选项,可以默认通过索引主题打开文档分类。在那之前,我使用了以下 JavaScript 变通方法:

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer((api) => {

    // 文档分类:将分类列表重定向到索引主题

    api.onPageChange(() => {
        
        const currentPath = window.location.pathname;
        const queryParams = window.location.search;
        
        const redirects = {
            
            "/c/category-1-slug/category-1-id": "/t/topic-1-slug/index-topic-1-id",
            "/c/category-2-slug/category-2-id": "/t/topic-2-slug/index-topic-2-id"
        };
        
        if (queryParams.includes("list=true")) return;
        
        const match = Object.keys(redirects).find(cat => currentPath.startsWith(cat));
        
        if (match) {
          const targetTopic = redirects[match];
          
          if (currentPath !== targetTopic) {

            const router = api.container.lookup("router:main");
            router.transitionTo(targetTopic);
          }
        }
    });

});

要以常规列表形式打开文档分类,请在 URL 中添加一个参数:

/c/category-1-slug/category-1-id?list=true
4 個讚