2024-01-21 22:23:05 +00:00
|
|
|
|
<!doctype html><html lang=zh-CN dir=ltr class=scroll-smooth data-default-appearance=light data-auto-appearance=true><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=theme-color content="rgb(255,255,255)"><title>高级定制 · Congo</title>
|
2024-04-17 00:42:14 +00:00
|
|
|
|
<meta name=title content="高级定制 · Congo"><script type=text/javascript src=/congo/js/appearance.min.74ad8406faea02f3e186ba5126249aaeed9073629e04b05037b903396b188724.js integrity="sha256-dK2EBvrqAvPhhrpRJiSaru2Qc2KeBLBQN7kDOWsYhyQ="></script><link type=text/css rel=stylesheet href=/congo/css/main.bundle.min.9602fe0216290b9ff0322c1fd14e88ab9fc0a13741f4de563f67831708d72bb2.css integrity="sha256-lgL+AhYpC5/wMiwf0U6Iq5/AoTdB9N5WP2eDFwjXK7I="><script defer type=text/javascript id=script-bundle src=/congo/js/main.bundle.min.5ff2b3a0b729a3f13a3d681740c924c9009e2be09e22a8458e486651e9e1af40.js integrity="sha256-X/KzoLcpo/E6PWgXQMkkyQCeK+CeIqhFjkhmUenhr0A=" data-copy=复制 data-copied=已复制></script><meta name=description content="
|
2024-01-21 22:23:05 +00:00
|
|
|
|
学习如何手动构建 Congo。
|
2024-04-17 00:42:14 +00:00
|
|
|
|
"><link rel=canonical href=https://jpanther.github.io/congo/zh-cn/docs/advanced-customisation/><link rel=apple-touch-icon sizes=180x180 href=/congo/apple-touch-icon.png><link rel=icon type=image/png sizes=32x32 href=/congo/favicon-32x32.png><link rel=icon type=image/png sizes=16x16 href=/congo/favicon-16x16.png><link rel=manifest href=/congo/site.webmanifest><meta property="og:url" content="https://jpanther.github.io/congo/zh-cn/docs/advanced-customisation/"><meta property="og:site_name" content="Congo"><meta property="og:title" content="高级定制"><meta property="og:description" content="学习如何手动构建 Congo。"><meta property="og:locale" content="zh-CN"><meta property="og:type" content="article"><meta property="article:section" content="docs"><meta property="article:tag" content="高级"><meta property="article:tag" content="Css"><meta property="article:tag" content="文档"><meta name=twitter:card content="summary"><meta name=twitter:title content="高级定制"><meta name=twitter:description content="学习如何手动构建 Congo。"><script type=application/ld+json>{"@context":"https://schema.org","@type":"Article","articleSection":"文档","name":"高级定制","headline":"高级定制","description":"学习如何手动构建 Congo。","abstract":"Congo 支持高级定制,包括修改底层的 Tailwind 配置,手动构建主题以及提供自定义 CSS。","inLanguage":"zh-CN","url":"https:\/\/jpanther.github.io\/congo\/zh-cn\/docs\/advanced-customisation\/","author":{"@type":"Person","name":"Congo"},"keywords":["高级","CSS","文档"],"mainEntityOfPage":"true","wordCount":"398"}</script><meta name=author content="Congo"><link href=https://twitter.com/ rel=me><link href=https://facebook.com/ rel=me><link href=https://linkedin.com/ rel=me><link href=https://youtube.com/ rel=me></head><body class="m-auto flex h-screen max-w-7xl flex-col bg-neutral px-6 text-lg leading-7 text-neutral-900 sm:px-14 md:px-24 lg:px-32 dark:bg-neutral-800 dark:text-neutral"><div id=the-top class="absolute flex self-center"><a class="-translate-y-8 rounded-b-lg bg-primary-200 px-3 py-1 text-sm focus:translate-y-0 dark:bg-neutral-600" href=#main-content><span class="pe-2 font-bold text-primary-600 dark:text-primary-400">↓</span>跳到主要内容</a></div><header class="py-6 font-semibold text-neutral-900 print:hidden sm:py-10 dark:text-neutral"><nav class="flex items-start justify-between sm:items-center"><div class="flex flex-row items-center"><a class="decoration-primary-500 hover:underline hover:decoration-2 hover:underline-offset-2" rel=me href=/congo/zh-cn/>Congo</a></div><ul class="flex list-none flex-col text-end sm:flex-row"><li class="group mb-1 sm:mb-0 sm:me-7 sm:last:me-0.5"><a href=/congo/zh-cn/docs/ title><span class="decoration-primary-500 group-hover:underline group-hover:decoration-2 group-hover:underline-offset-2">文档</span></a></li><li class="group mb-1 sm:mb-0 sm:me-7 sm:last:me-0.5"><a href=/congo/zh-cn/samples/ title><span class="decoration-primary-500 group-hover:underline group-hover:decoration-2 group-hover:underline-offset-2">示例</span></a></li><li class="group mb-1 sm:mb-0 sm:me-7 sm:last:me-0.5"><a href=/congo/zh-cn/users/ title><span class="decoration-primary-500 group-hover:underline group-hover:decoration-2 group-hover:underline-offset-2">用户</span></a></li><li class="group mb-1 sm:mb-0 sm:me-7 sm:last:me-0.5"><a href=https://github.com/jpanther/congo title target=_blank><span class="group-dark:hover:text-primary-400 transition-colors group-hover:text-primary-600"><span class="icon relative inline-block px-1 align-text-bottom"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><path fill="currentcolor" d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6.0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6.0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3.0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 1
|
2024-03-09 04:37:45 +00:00
|
|
|
|
<span class="group-dark:hover:text-primary-400 transition-colors group-hover:text-primary-600"><span class="icon relative inline-block px-1 align-text-bottom"><svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="search" class="svg-inline--fa fa-search fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentcolor" d="M505 442.7 405.3 343c-4.5-4.5-10.6-7-17-7H372c27.6-35.3 44-79.7 44-128C416 93.1 322.9.0 208 0S0 93.1.0 208s93.1 208 208 208c48.3.0 92.7-16.4 128-44v16.3c0 6.4 2.5 12.5 7 17l99.7 99.7c9.4 9.4 24.6 9.4 33.9.0l28.3-28.3c9.4-9.4 9.4-24.6.1-34zM208 336c-70.7.0-128-57.2-128-128 0-70.7 57.2-128 128-128 70.7.0 128 57.2 128 128 0 70.7-57.2 128-128 128z"/></svg>
|
|
|
|
|
</span></span><span class="decoration-primary-500 group-hover:underline group-hover:decoration-2 group-hover:underline-offset-2"></span></button></li><li class="group mb-1 sm:mb-0 sm:me-7 sm:last:me-0.5"><div class="group relative"><button class="group-dark:hover:text-primary-400 flex w-full items-center justify-end transition-colors group-hover:text-primary-600">
|
|
|
|
|
<span class="icon relative inline-block px-1 align-text-bottom"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path fill="currentcolor" d="M4.545 6.714 4.11 8H3l1.862-5h1.284L8 8H6.833l-.435-1.286H4.545zm1.634-.736L5.5 3.956h-.049l-.679 2.022H6.18z"/><path fill="currentcolor" d="M0 2a2 2 0 012-2h7a2 2 0 012 2v3h3a2 2 0 012 2v7a2 2 0 01-2 2H7a2 2 0 01-2-2v-3H2A2 2 0 010 9V2zm2-1A1 1 0 001 2v7a1 1 0 001 1h7a1 1 0 001-1V2A1 1 0 009 1H2zm7.138 9.995c.193.301.402.583.63.846-.748.575-1.673 1.001-2.768 1.292.178.217.451.635.555.867 1.125-.359 2.08-.844 2.886-1.494.777.665 1.739 1.165 2.93 1.472.133-.254.414-.673.629-.89-1.125-.253-2.057-.694-2.82-1.284.681-.747 1.222-1.651 1.621-2.757H14V8h-3v1.047h.765c-.318.844-.74 1.546-1.272 2.13a6.066 6.066.0 01-.415-.492 1.988 1.988.0 01-.94.31z"/></svg>
|
|
|
|
|
</span><span class=text-sm>ZH-CN</span><span class=text-[0.6rem]><span class="icon relative inline-block px-1 align-text-bottom"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentcolor" d="M233.4 406.6c12.5 12.5 32.8 12.5 45.3.0l192-192c12.5-12.5 12.5-32.8.0-45.3s-32.8-12.5-45.3.0L256 338.7 86.6 169.4c-12.5-12.5-32.8-12.5-45.3.0s-12.5 32.8.0 45.3l192 192z"/></svg></span></span></button><div class="invisible w-full bg-transparent group-hover:visible"></div><div class="invisible absolute z-50 flex flex-col whitespace-nowrap rounded border border-neutral-300 bg-neutral text-start text-base shadow group-hover:visible ltr:right-0 rtl:left-0 dark:border-neutral-600 dark:bg-neutral-800"><div class="flex flex-grow"><a href=/congo/docs/advanced-customisation/ class="w-full py-1 pe-10 ps-2 decoration-primary-500 hover:underline hover:decoration-2 hover:underline-offset-2">English (Australia)</a></div><div class="flex flex-grow"><a href=/congo/ja/docs/advanced-customisation/ class="w-full py-1 pe-10 ps-2 decoration-primary-500 hover:underline hover:decoration-2 hover:underline-offset-2">日本語</a></div><div class="flex flex-grow"><a href=/congo/zh-cn/docs/advanced-customisation/ class="flex w-full items-center justify-between bg-primary-100 px-2 py-1 dark:bg-primary-900">简体中文<span class="ms-2 w-6 text-primary-600 dark:text-primary-400"><span class="icon relative inline-block px-1 align-text-bottom"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path fill="currentcolor" d="M438.6 105.4c12.5 12.5 12.5 32.7.0 45.2l-256 256c-12.5 12.5-32.7 12.5-45.2.0L9.372 278.6c-12.496-12.5-12.496-32.7.0-45.2 12.498-12.5 32.758-12.5 45.258.0L159.1 338.7 393.4 105.4c12.5-12.52 32.7-12.52 45.2.0h0z"/></svg></span></span></a></div></div></div></li></ul></nav></header><div class="relative flex grow flex-col"><main id=main-content class=grow><article><header class=max-w-prose><ol class="text-sm text-neutral-500 print:hidden dark:text-neutral-400"><li class="hidden inline"><a class="dark:underline-neutral-600 decoration-neutral-300 hover:underline" href=/congo/zh-cn/>欢迎使用Congo! :tada:</a><span class="px-1 text-primary-500">/</span></li><li class=inline><a class="dark:underline-neutral-600 decoration-neutral-300 hover:underline" href=/congo/zh-cn/docs/>文档</a><span class="px-1 text-primary-500">/</span></li><li class="hidden inline"><a class="dark:underline-neutral-600 decoration-neutral-300 hover:underline" href=/congo/zh-cn/docs/advanced-customisation/>高级定制</a><span class="px-1 text-primary-500">/</span></li></ol><h1 class="mt-0 text-4xl font-extrabold text-neutral-900 dark:text-neutral">高级定制</h1><div class="mb-12 mt-8 text-base text-neutral-500 print:hidden dark:text-neutral-400"><div class="flex flex-row flex-wrap items-center"><span title=预计阅读>2 分钟</span><span class="px-2 text-primary-500">·</span>
|
|
|
|
|
<span class=mb-[2px]><a href=https://github.com/jpanther/congo/tree/dev/exampleSite/content/docs/advanced-customisation/index.zh-cn.md class="text-lg hover:text-primary-500" rel="noopener noreferrer" target=_blank title=编辑内容><span class="icon relative inline-block px-1 align-text-bottom"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentcolor" d="M490.3 40.4c21.9 21.87 21.9 57.33.0 79.2l-30 30.1-98-97.98 30.1-30.06C414.3-.2135 449.7-.2135 471.6 21.66L490.3 40.4zM172.4 241.7 339.7 74.34l98 97.96L270.3 339.6C264.2 345.8 256.7 350.4 248.4 353.2l-88.8 29.6C150.1 385.6 141.5 383.4 135 376.1 128.6 370.5 126.4 361 129.2 352.4l29.6-88.8C161.6 255.3 166.2 247.8 172.4 241.7v0zM192 63.1c17.7.0 32 15.23 32 32 0 18.6-14.3 32-32 32H96c-17.67.0-32 15.2-32 32V416c0 17.7 14.33 32 32 32H352c17.7.0 32-14.3 32-32V319.1c0-16.8 14.3-32 32-32s32 15.2 32 32V416c0 53-43 96-96 96H96c-53.02.0-96-43-96-96V159.1c0-53 42.98-96 96-96h96z"/></svg></span></a></span></div></div></header><section class="prose mt-0 flex max-w-full flex-col dark:prose-invert lg:flex-row"><div class="order-first px-0 lg:order-last lg:max-w-xs lg:ps-8"><div class="toc pe-5 print:hidden lg:sticky lg:top-10"><details open class="-ms-5 mt-0 overflow-hidden rounded-lg ps-5"><summary class="block cursor-pointer bg-neutral-100 py-1 ps-5 text-lg font-semibold text-neutral-800 dark:bg-neutral-700 dark:text-neutral-100 lg:hidden">目录</summary><div class="border-s border-dotted border-neutral-300 py-2 ps-5 dark:border-neutral-600"><nav id=TableOfContents><ul><li><a href=#hugo-项目结构>Hugo 项目结构</a></li><li><a href=#颜色方案>颜色方案</a></li><li><a href=#覆盖样式表>覆盖样式表</a><ul><li><a href=#调整字体大小>调整字体大小</a></li></ul></li><li><a href=#从源代码构建主题-css>从源代码构建主题 CSS</a><ul><li><a href=#tailwind-配置>Tailwind 配置</a></li><li><a href=#项目结构>项目结构</a></li><li><a href=#安装依赖项>安装依赖项</a></li><li><a href=#运行-tailwind-编译器>运行 Tailwind 编译器</a></li><li><a href=#创建构建脚本>创建构建脚本</a></li></ul></li></ul></nav></div></details></div></div><div class="min-h-0 min-w-0 max-w-prose grow"><p>有许多方法可以对 Congo 进行高级更改。阅读以下内容,了解可以定制的内容以及实现所需结果的最佳方式。</p><p>如果您需要进一步的建议,请在 <a href=https://github.com/jpanther/congo/discussions target=_blank rel=noreferrer>GitHub 讨论区</a> 上发表您的问题。</p><h2 id=hugo-项目结构 class="relative group">Hugo 项目结构 <span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"><a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style=text-decoration-line:none!important href=#hugo-%e9%a1%b9%e7%9b%ae%e7%bb%93%e6%9e%84 aria-label=锚点>#</a></span></h2><p>在深入了解之前,首先简要介绍一下<a href=https://gohugo.io/getting-started/directory-structure/ target=_blank rel=noreferrer>Hugo 项目结构</a>和管理内容以及主题定制的最佳实践。</p><div class="flex rounded-md bg-primary-100 px-4 py-3 dark:bg-primary-900"><span class="pe-3 text-primary-400"><span class="icon relative inline-block px-1 align-text-bottom"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentcolor" d="M506.3 417 293 53c-16.33-28-57.54-28-73.98.0l-213.2 364C-10.59 444.9 9.849 480 42.74 480h426.6c32.76.0 53.26-35 36.96-63zM232 168c0-13.25 10.75-24 24-24s24 10.8 24 24v128c0 13.25-10.75 24-23.1 24S232 309.3 232 296V168zm24 248c-17.36.0-31.44-14.08-31.44-31.44s14.07-31.44 31.44-31.44 31.44 14.08 31.44 31.44C287.4 401.9 273.4 416 256 416z"/></svg>
|
|
|
|
|
</span></span><span class=dark:text-neutral-300>**总结:**永远不要直接编辑主题文件。只在 Hugo 项目的子目录中进行定制,而不要在主题目录本身进行定制。</span></div><p>Congo 的构建旨在充分利用所有标准 Hugo 实践。它被设计为允许定制和覆盖主题的所有方面,而无需更改任何核心主题文件。这样一来,您可以在完全控制网站的外观和感觉的同时,获得无缝的升级体验。</p><p>为了实现这一点,您永远不应直接调整主题文件中的任何文件。无论您是使用 Hugo 模块安装,作为 git 子模块或手动将主题包含在您的 <code>themes/</code> 目录中,都应始终保持这些文件不变。</p><p>调整任何主题行为的正确方式是使用 Hugo 强大的<a href=https://gohugo.io/templates/lookup-order/ target=_blank rel=noreferrer>文件查找顺序</a>。总体而言,查找顺序确保您在项目目录中包含的任何文件将自动优先于任何主题文件。</p><p>例如,如果您想要覆盖 Congo 中的主文章模板,您只需创建自己的 <code>layouts/_default/single.html</code> 文件并将其放置在项目的根目录。该文件将覆盖主题中的 <code>single.html</code>,而无需更改主题本身。这适用于任何主题文件 - HTML 模板、局部、短代码、配置文件、数据、资产等。</p><p>只要遵循这个简单的做法,您就始终能够更新主题(或测试不同的主题版本),而不必担心会丢失任何自定义更改。</p><h2 id=颜色方案 class="relative group">颜色方案 <span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"><a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style=text-decoration-line:none!important href=#%e9%a2%9c%e8%89%b2%e6%96%b9%e6%a1%88 aria-label=锚点>#</a></span></h2><p>Congo 默认提供了许多颜色方案。要更改基本的颜色方案,您可以设置 <code>colorScheme</code> 主题参数。请参阅 <a href=https://jpanther.github.io/congo/zh-cn/docs/getting-started/#colour-schemes>入门指南</a> 部分,了解内置方案的更多信息。</p><p>除了默认方案之外,您还可以创建自己的方案,并根据自己的喜好重新设计整个网站。通过在 <code>assets/css/schemes/</code> 文件夹中放置 <code><scheme-name>.css</code> 文件来创建方案。创建文件后,只需在主题配置中按名称引用即可。</p><p>Congo 定义了一个贯穿整个主题使用的三种颜色的调色板。这三种颜色分别被定义为 <code>neutral</code>、<code>primary</code> 和 <code>secondary</code> 变体,每种颜色包含十个色调。</p><p>由于 Tailwind CSS 3.0 使用不透明度计算颜色值的方式,方案中指定的颜色需要<a href=https://github.com/adamwathan/tailwind-css-variable-text-opacity-demo target=_blank rel=noreferrer>符合特定格式</a>,即提供红色、绿色和蓝色颜色值。</p><div class=highlight><pre tabindex=0 class=chroma><code class=language-css data-lang=css><span class=line><span class=cl><span class=p>:</span><span class=nd>root</span> <span class=p>{</span>
|
2024-01-21 22:23:05 +00:00
|
|
|
|
</span></span><span class=line><span class=cl> <span class=nv>--color-primary-500</span><span class=p>:</span> <span class=mi>139</span><span class=p>,</span> <span class=mi>92</span><span class=p>,</span> <span class=mi>246</span><span class=p>;</span>
|
|
|
|
|
</span></span><span class=line><span class=cl><span class=p>}</span>
|
2024-03-09 04:37:45 +00:00
|
|
|
|
</span></span></code></pre></div><p>此示例为 <code>primary-500</code> 颜色定义了一个 CSS 变量,其红色值为 <code>139</code>,绿色值为 <code>92</code>,蓝色值为 <code>246</code>。</p><p>使用现有的主题样式表作为模板。您可以自定义自己的颜色,但是为了一些灵感,请查看官方的 <a href=https://tailwindcss.com/docs/customizing-colors#color-palette-reference target=_blank rel=noreferrer>Tailwind 颜色调色板参考</a>。</p><h2 id=覆盖样式表 class="relative group">覆盖样式表 <span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"><a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style=text-decoration-line:none!important href=#%e8%a6%86%e7%9b%96%e6%a0%b7%e5%bc%8f%e8%a1%a8 aria-label=锚点>#</a></span></h2><p>有时您需要添加自定义样式以为自己的 HTML 元素设置样式。Congo 提供了允许您在自己的 CSS 样式表中覆盖默认样式的场景。只需在项目的 <code>assets/css/</code> 文件夹中创建一个 <code>custom.css</code> 文件。</p><p><code>custom.css</code> 文件将由 Hugo 进行最小化,并在所有其他主题样式之后自动加载,这意味着自定义文件中的任何内容都将覆盖默认值。</p><h3 id=调整字体大小 class="relative group">调整字体大小 <span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"><a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style=text-decoration-line:none!important href=#%e8%b0%83%e6%95%b4%e5%ad%97%e4%bd%93%e5%a4%a7%e5%b0%8f aria-label=锚点>#</a></span></h3><p>更改网站的字体大小是覆盖默认样式表的一个示例。Congo 使此变得简单,因为它在整个主题中使用了从基本 HTML 字体大小派生的缩放字体大小。默认情况下,Tailwind 将默认大小设置为 <code>12pt</code>,但可以更改为您喜欢的任何值。</p><p>使用上述<a href=#overriding-the-stylesheet>说明</a>创建一个 <code>custom.css</code> 文件,并添加以下 CSS 声明:</p><div class=highlight><pre tabindex=0 class=chroma><code class=language-css data-lang=css><span class=line><span class=cl><span class=c>/* Increase the default font size */</span>
|
2024-01-21 22:23:05 +00:00
|
|
|
|
</span></span><span class=line><span class=cl><span class=nt>html</span> <span class=p>{</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=k>font-size</span><span class=p>:</span> <span class=mi>13</span><span class=kt>pt</span><span class=p>;</span>
|
|
|
|
|
</span></span><span class=line><span class=cl><span class=p>}</span>
|
2024-03-09 04:37:45 +00:00
|
|
|
|
</span></span></code></pre></div><p>只需更改此一个值,您网站上的所有字体大小就会调整为匹配这个新大小。因此,要增加使用的整体字体大小,请将值设置为大于 <code>12pt</code>。同样,要减小字体大小,请将值设置为小于 <code>12pt</code>。</p><h2 id=从源代码构建主题-css class="relative group">从源代码构建主题 CSS <span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"><a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style=text-decoration-line:none!important href=#%e4%bb%8e%e6%ba%90%e4%bb%a3%e7%a0%81%e6%9e%84%e5%bb%ba%e4%b8%bb%e9%a2%98-css aria-label=锚点>#</a></span></h2><p>如果您想进行重大更改,可以利用 Tailwind CSS 的 JIT 编译器从头开始重新构建整个主题 CSS。如果您想要调整 Tailwind 配置或向主样式表添加额外的 Tailwind 类,这将非常有用。</p><div class="flex rounded-md bg-primary-100 px-4 py-3 dark:bg-primary-900"><span class="pe-3 text-primary-400"><span class="icon relative inline-block px-1 align-text-bottom"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentcolor" d="M506.3 417 293 53c-16.33-28-57.54-28-73.98.0l-213.2 364C-10.59 444.9 9.849 480 42.74 480h426.6c32.76.0 53.26-35 36.96-63zM232 168c0-13.25 10.75-24 24-24s24 10.8 24 24v128c0 13.25-10.75 24-23.1 24S232 309.3 232 296V168zm24 248c-17.36.0-31.44-14.08-31.44-31.44s14.07-31.44 31.44-31.44 31.44 14.08 31.44 31.44C287.4 401.9 273.4 416 256 416z"/></svg>
|
|
|
|
|
</span></span><span class=dark:text-neutral-300><strong>注意:</strong> 手动构建主题仅适用于高级用户。</span></div><p>让我们逐步了解构建 Tailwind CSS 的过程。</p><h3 id=tailwind-配置 class="relative group">Tailwind 配置 <span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"><a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style=text-decoration-line:none!important href=#tailwind-%e9%85%8d%e7%bd%ae aria-label=锚点>#</a></span></h3><p>为了生成一个仅包含实际使用的 Tailwind 类的 CSS 文件,JIT 编译器需要扫描所有 HTML 模板和 Markdown 内容文件,以检查标记中存在哪些样式。编译器通过查看主题目录根目录中包含的 <code>tailwind.config.js</code> 文件来执行此操作:</p><div class=highlight><pre tabindex=0 class=chroma><code class=language-js data-lang=js><span class=line><span class=cl><span class=c1>// themes/congo/tailwind.config.js
|
2024-01-21 22:23:05 +00:00
|
|
|
|
</span></span></span><span class=line><span class=cl><span class=c1></span>
|
|
|
|
|
</span></span><span class=line><span class=cl><span class=nx>module</span><span class=p>.</span><span class=nx>exports</span> <span class=o>=</span> <span class=p>{</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=nx>content</span><span class=o>:</span> <span class=p>[</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=s2>"./layouts/**/*.html"</span><span class=p>,</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=s2>"./content/**/*.{html,md}"</span><span class=p>,</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=s2>"./themes/congo/layouts/**/*.html"</span><span class=p>,</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=s2>"./themes/congo/content/**/*.{html,md}"</span><span class=p>,</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=p>],</span>
|
|
|
|
|
</span></span><span class=line><span class=cl>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=c1>// and more...
|
|
|
|
|
</span></span></span><span class=line><span class=cl><span class=c1></span><span class=p>};</span>
|
2024-03-09 04:37:45 +00:00
|
|
|
|
</span></span></code></pre></div><p>这个默认配置已经包含了这些内容路径,以便您可以轻松生成自己的 CSS 文件,而无需修改它,只要您遵循特定的项目结构。换句话说,<strong>您必须将 Congo 作为子目录包含在项目中,即 <code>themes/congo/</code></strong>。这意味着您不能轻松使用 Hugo 模块来安装主题,而必须选择 git 子模块(推荐)或手动安装路线。<a href=https://jpanther.github.io/congo/zh-cn/docs/installation/>安装文档</a>解释了如何使用这两种方法之一安装主题。</p><h3 id=项目结构 class="relative group">项目结构 <span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"><a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style=text-decoration-line:none!important href=#%e9%a1%b9%e7%9b%ae%e7%bb%93%e6%9e%84 aria-label=锚点>#</a></span></h3><p>为了充分利用默认配置,您的项目结构应该长得像下面这个样子…</p><div class=highlight><pre tabindex=0 class=chroma><code class=language-shell data-lang=shell><span class=line><span class=cl>.
|
2024-01-21 22:23:05 +00:00
|
|
|
|
</span></span><span class=line><span class=cl>├── assets
|
|
|
|
|
</span></span><span class=line><span class=cl>│ └── css
|
|
|
|
|
</span></span><span class=line><span class=cl>│ └── compiled
|
|
|
|
|
</span></span><span class=line><span class=cl>│ └── main.css <span class=c1># this is the file we will generate</span>
|
|
|
|
|
</span></span><span class=line><span class=cl>├── config <span class=c1># site config</span>
|
|
|
|
|
</span></span><span class=line><span class=cl>│ └── _default
|
|
|
|
|
</span></span><span class=line><span class=cl>├── content <span class=c1># site content</span>
|
|
|
|
|
</span></span><span class=line><span class=cl>│ ├── _index.md
|
|
|
|
|
</span></span><span class=line><span class=cl>│ ├── projects
|
|
|
|
|
</span></span><span class=line><span class=cl>│ │ └── _index.md
|
|
|
|
|
</span></span><span class=line><span class=cl>│ └── blog
|
|
|
|
|
</span></span><span class=line><span class=cl>│ └── _index.md
|
|
|
|
|
</span></span><span class=line><span class=cl>├── layouts <span class=c1># custom layouts for your site</span>
|
|
|
|
|
</span></span><span class=line><span class=cl>│ ├── partials
|
|
|
|
|
</span></span><span class=line><span class=cl>│ │ └── extend-article-link.html
|
|
|
|
|
</span></span><span class=line><span class=cl>│ ├── projects
|
|
|
|
|
</span></span><span class=line><span class=cl>│ │ └── list.html
|
|
|
|
|
</span></span><span class=line><span class=cl>│ └── shortcodes
|
|
|
|
|
</span></span><span class=line><span class=cl>│ └── disclaimer.html
|
|
|
|
|
</span></span><span class=line><span class=cl>└── themes
|
|
|
|
|
</span></span><span class=line><span class=cl> └── congo <span class=c1># git submodule or manual theme install</span>
|
2024-03-09 04:37:45 +00:00
|
|
|
|
</span></span></code></pre></div><p>这个例子的结构添加了一个新的 <code>projects</code> 内容类型,具有自己的自定义布局,以及一个自定义短代码和扩展部分。只要项目遵循这个结构,唯一需要做的就是重新编译 <code>main.css</code> 文件。</p><h3 id=安装依赖项 class="relative group">安装依赖项 <span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"><a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style=text-decoration-line:none!important href=#%e5%ae%89%e8%a3%85%e4%be%9d%e8%b5%96%e9%a1%b9 aria-label=锚点>#</a></span></h3><p>为了使这个工作,你需要切换到 <code>themes/congo/</code> 目录并安装项目的依赖项。对于这一步骤,你的本地机器上需要 <a href=https://docs.npmjs.com/cli/v7/configuring-npm/install target=_blank rel=noreferrer>npm</a>。</p><div class=highlight><pre tabindex=0 class=chroma><code class=language-shell data-lang=shell><span class=line><span class=cl><span class=nb>cd</span> themes/congo
|
2024-01-21 22:23:05 +00:00
|
|
|
|
</span></span><span class=line><span class=cl>npm install
|
2024-03-09 04:37:45 +00:00
|
|
|
|
</span></span></code></pre></div><h3 id=运行-tailwind-编译器 class="relative group">运行 Tailwind 编译器 <span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"><a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style=text-decoration-line:none!important href=#%e8%bf%90%e8%a1%8c-tailwind-%e7%bc%96%e8%af%91%e5%99%a8 aria-label=锚点>#</a></span></h3><p>在安装了依赖项之后,唯一需要做的就是使用 <a href=https://v2.tailwindcss.com/docs/installation#using-tailwind-cli target=_blank rel=noreferrer>Tailwind CLI</a> 调用 JIT 编译器。返回到你的 Hugo 项目的根目录,执行以下命令:</p><div class=highlight><pre tabindex=0 class=chroma><code class=language-shell data-lang=shell><span class=line><span class=cl><span class=nb>cd</span> ../..
|
2024-01-21 22:23:05 +00:00
|
|
|
|
</span></span><span class=line><span class=cl>./themes/congo/node_modules/tailwindcss/lib/cli.js -c ./themes/congo/tailwind.config.js -i ./themes/congo/assets/css/main.css -o ./assets/css/compiled/main.css --jit
|
|
|
|
|
</span></span></code></pre></div><p>由于涉及到路径,这是一个有点丑陋的命令,但基本上你是在调用 Tailwind CLI 并传递 Tailwind 配置文件的位置(我们上面看到的文件),主题的 <code>main.css</code> 文件的位置,以及你想要放置编译后的 CSS 文件的位置(它将放在你的 Hugo 项目的 <code>assets/css/compiled/</code> 文件夹中)。</p><p>配置文件将自动检查你的项目中的所有内容和布局,以及主题中的所有内容,并构建一个新的 CSS 文件,其中包含你的网站所需的所有 CSS。由于 Hugo 处理文件层次结构的方式,这个文件在你的项目中现在将自动覆盖主题自带的文件。</p><p>每当你更改布局并需要新的 Tailwind CSS 样式时,只需重新运行该命令并生成新的 CSS 文件。你也可以在命令的末尾添加 <code>-w</code> 以在监视模式下运行 JIT 编译器。</p><h3 id=创建构建脚本 class="relative group">创建构建脚本 <span class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100"><a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700" style=text-decoration-line:none!important href=#%e5%88%9b%e5%bb%ba%e6%9e%84%e5%bb%ba%e8%84%9a%e6%9c%ac aria-label=锚点>#</a></span></h3><p>为了完全完成这个解决方案,你可以通过为这些命令添加别名,或者像我一样,在你的项目根目录添加一个包含必要脚本的 <code>package.json</code> 文件,来简化整个过程…</p><div class=highlight><pre tabindex=0 class=chroma><code class=language-js data-lang=js><span class=line><span class=cl><span class=c1>// package.json
|
|
|
|
|
</span></span></span><span class=line><span class=cl><span class=c1></span>
|
|
|
|
|
</span></span><span class=line><span class=cl><span class=p>{</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=s2>"name"</span><span class=o>:</span> <span class=s2>"my-website"</span><span class=p>,</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=s2>"version"</span><span class=o>:</span> <span class=s2>"1.0.0"</span><span class=p>,</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=s2>"description"</span><span class=o>:</span> <span class=s2>""</span><span class=p>,</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=s2>"scripts"</span><span class=o>:</span> <span class=p>{</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=s2>"server"</span><span class=o>:</span> <span class=s2>"hugo server -b http://localhost -p 8000"</span><span class=p>,</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=s2>"dev"</span><span class=o>:</span> <span class=s2>"NODE_ENV=development ./themes/congo/node_modules/tailwindcss/lib/cli.js -c ./themes/congo/tailwind.config.js -i ./themes/congo/assets/css/main.css -o ./assets/css/compiled/main.css --jit -w"</span><span class=p>,</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=s2>"build"</span><span class=o>:</span> <span class=s2>"NODE_ENV=production ./themes/congo/node_modules/tailwindcss/lib/cli.js -c ./themes/congo/tailwind.config.js -i ./themes/congo/assets/css/main.css -o ./assets/css/compiled/main.css --jit"</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=p>},</span>
|
|
|
|
|
</span></span><span class=line><span class=cl> <span class=c1>// and more...
|
|
|
|
|
</span></span></span><span class=line><span class=cl><span class=c1></span><span class=p>}</span>
|
2024-03-09 04:37:45 +00:00
|
|
|
|
</span></span></code></pre></div><p>现在,当你想要设计你的网站时,你可以调用 <code>npm run dev</code>,编译器将在监视模式下运行。当你准备部署时,运行 <code>npm run build</code>,你将得到一个干净的 Tailwind CSS 构建。</p><p>🙋♀️ 如果你需要帮助,请随时在 <a href=https://github.com/jpanther/congo/discussions target=_blank rel=noreferrer>GitHub Discussions</a> 上提问。</p></div></section><footer class="max-w-prose pt-8 print:hidden"><div class=pt-8><hr class="border-dotted border-neutral-300 dark:border-neutral-600"><div class="flex justify-between pt-3"><span><a class="group flex" href=/congo/zh-cn/docs/hosting-deployment/><span class="me-2 text-neutral-700 transition-transform group-hover:-translate-x-[2px] group-hover:text-primary-600 dark:text-neutral dark:group-hover:text-primary-400"><span class="ltr:inline rtl:hidden">←</span><span class="ltr:hidden rtl:inline">→</span></span>
|
2024-01-21 22:23:05 +00:00
|
|
|
|
<span class="flex flex-col"><span class="mt-[0.1rem] leading-6 group-hover:underline group-hover:decoration-primary-500">部署</span>
|
|
|
|
|
<span class="mt-[0.1rem] text-xs text-neutral-500 dark:text-neutral-400"></span></span></a></span><span></span></div></div></footer></article><div class="pointer-events-none absolute bottom-0 end-0 top-[100vh] w-12"><a href=#the-top class="pointer-events-auto sticky top-[calc(100vh-5.5rem)] flex h-12 w-12 items-center justify-center rounded-full bg-neutral/50 text-xl text-neutral-700 backdrop-blur hover:text-primary-600 dark:bg-neutral-800/50 dark:text-neutral dark:hover:text-primary-400" aria-label=回到顶部 title=回到顶部>↑</a></div></main><footer class="py-10 print:hidden"><div class="flex items-center justify-between"><div><p class="text-sm text-neutral-500 dark:text-neutral-400">© 2023 Congo contributors</p><p class="text-xs text-neutral-500 dark:text-neutral-400">由 <a class="hover:underline hover:decoration-primary-400 hover:text-primary-500" href=https://gohugo.io/ target=_blank rel="noopener noreferrer">Hugo</a> & <a class="hover:underline hover:decoration-primary-400 hover:text-primary-500" href=https://github.com/jpanther/congo target=_blank rel="noopener noreferrer">Congo</a> 强力驱动</p></div><div class="flex flex-row items-center"><div class="me-14 cursor-pointer text-sm text-neutral-700 hover:text-primary-600 dark:text-neutral dark:hover:text-primary-400"><button id=appearance-switcher-0 type=button aria-label="appearance switcher"><div class="flex h-12 w-12 items-center justify-center dark:hidden" title=切换为深色模式><span class="icon relative inline-block px-1 align-text-bottom"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentcolor" d="M32 256C32 132.2 132.3 32 255.8 32c11.36.0 29.7 1.668 40.9 3.746 9.616 1.777 11.75 14.63 3.279 19.44C245 86.5 211.2 144.6 211.2 207.8c0 109.7 99.71 193 208.3 172.3 9.561-1.805 16.28 9.324 10.11 16.95C387.9 448.6 324.8 480 255.8 480 132.1 480 32 379.6 32 256z"/></svg></span></div><div class="hidden h-12 w-12 items-center justify-center dark:flex" title=切换为浅色模式><span class="icon relative inline-block px-1 align-text-bottom"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentcolor" d="M256 159.1c-53.02.0-95.1 42.98-95.1 95.1s41.2 96.9 95.1 96.9 95.1-42.98 95.1-95.1S309 159.1 256 159.1zM509.3 347l-63.2-91.9 63.15-91.01c6.332-9.125 1.104-21.74-9.826-23.72l-109-19.7-19.7-109c-1.975-10.93-14.59-16.16-23.72-9.824L256 65.89 164.1 2.736c-9.125-6.332-21.74-1.107-23.72 9.824L121.6 121.6 12.56 141.3C1.633 143.2-3.596 155.9 2.736 164.1L65.89 256 2.74 347.01c-6.332 9.125-1.105 21.74 9.824 23.72l109 19.7 19.7 109c1.975 10.93 14.59 16.16 23.72 9.824L256 446.1l91.01 63.15c9.127 6.334 21.75 1.107 23.72-9.822l19.7-109 109-19.7C510.4 368.8 515.6 356.1 509.3 347zM256 383.1c-70.69.0-127.1-57.31-127.1-127.1.0-70.69 57.31-127.1 127.1-127.1S383.1 186.2 383.1 256c0 70.7-56.4 127.1-127.1 127.1z"/></svg></span></div></button></div></div></div></footer><div id=search-wrapper class="invisible fixed inset-0 z-50 flex h-screen w-screen cursor-default flex-col bg-neutral-500/50 p-4 backdrop-blur-sm sm:p-6 md:p-[10vh] lg:p-[12vh] dark:bg-neutral-900/50" data-url=https://jpanther.github.io/congo/zh-cn/><div id=search-modal class="top-20 mx-auto flex min-h-0 w-full max-w-3xl flex-col rounded-md border border-neutral-200 bg-neutral shadow-lg dark:border-neutral-700 dark:bg-neutral-800"><header class="relative z-10 flex flex-none items-center justify-between px-2"><form class="flex min-w-0 flex-auto items-center"><div class="flex h-8 w-8 items-center justify-center text-neutral-400"><span class="icon relative inline-block px-1 align-text-bottom"><svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="search" class="svg-inline--fa fa-search fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentcolor" d="M505 442.7 405.3 343c-4.5-4.5-10.6-7-17-7H372c27.6-35.3 44-79.7 44-128C416 93.1 322.9.0 208 0S0 93.1.0 208s93.1 208 208 208c48.3.0 92.7-16.4 128-44v16.3c0 6.4 2.5 12.5 7 17l99.7 99.7c9.4 9.4 24.6 9.4 33.9.0l28.3-28.3c9.4-9.4 9.4-24.6.1-34zM208 336c-70.7.0
|
|
|
|
|
<span class="icon relative inline-block px-1 align-text-bottom"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><path fill="currentcolor" d="M310.6 361.4c12.5 12.5 12.5 32.75.0 45.25C304.4 412.9 296.2 416 288 416s-16.38-3.125-22.62-9.375L160 301.3 54.63 406.6C48.38 412.9 40.19 416 32 416S15.63 412.9 9.375 406.6c-12.5-12.5-12.5-32.75.0-45.25l105.4-105.4L9.375 150.6c-12.5-12.5-12.5-32.75.0-45.25s32.75-12.5 45.25.0L160 210.8l105.4-105.4c12.5-12.5 32.75-12.5 45.25.0s12.5 32.75.0 45.25l-105.4 105.4L310.6 361.4z"/></svg></span></button></header><section class="flex-auto overflow-auto px-2"><ul id=search-results></ul></section></div></div></div></body></html>
|