创建并分享字体主题组件

Discourse supports importing themes containing assets from a remote repository.

This allows theme authors to share fonts and images.

Create a new git repository with the font

mkdir discourse-roboto-theme
cd discourse-roboto-theme
git init .
vim about.json

For about.json add a skeleton config file

{
  "name": "Roboto theme component",
  "about_url": "",
  "license_url": "",
  "assets": {
    "roboto": "assets/roboto.woff2"
  }
}

Add a LICENSE file, I usually use MIT

vim LICENSE

Download the font

http://localfont.com/ is a handy site to get fonts

mkdir assets
cp ~/Downloads/roboto.woff2 roboto.woff2

Add CSS that consumes the theme

mkdir common
cd common

Create a file called common.scss with

@font-face {
  font-family: Roboto;
  src: url($roboto) format("woff2");
}

body {
  font-family: Roboto;
}

Push changes to GitHub

Check in all your changes:

git add LICENSE
git add about.json
git add assets/roboto.woff2
git add common/common.scss
git commit -am "first commit"

Create an account on GitHub.com and then create a new repository.

(Optional) create a topic on Discourse as a home to discuss your colors

Ideally you would create a topic in the #plugin:theme category with some screenshots of your color scheme. You will use this as your about_url

Fill in the missing information in your about.json file

  • Navigate to your LICENSE page on GitHub, fill in that URL as your license_url

  • Either use the GitHub project URL or Discourse topic URL as your about_url

At the end of the process your about.json file will look something like:

{
  "name": "Roboto theme component",
  "about_url": "https://github.com/SamSaffron/discourse-roboto-theme",
  "license_url": "https://github.com/SamSaffron/discourse-roboto-theme/blob/master/LICENSE",
  "assets": {
    "roboto": "assets/roboto.woff2"
  }
}

Check in the change and push to GitHub

git commit -am "added more details"
git push

Test your font component

  • In the admin/customize/theme screen import your theme from GitHub

:confetti_ball:

You can now easily share fonts!

See also:


This document is version controlled - suggest changes on github.

19 个赞

Great tutorial.

Worked like a charm except it looks like localfont.com is down or no more.

I used this Google webfonts helper site to download a .woff2 version of the font I was after.

See also Discourse Theme CLI (console app to help you build themes)

1 个赞

当我按照步骤操作时,我收到了一个错误:未定义变量:“$fontname”。

所以:
我在 $variables 中声明了变量:
fontname: "assets/fontname.woff2";

看起来您需要填写它,但应该在 $variables 中使用什么路径?

当您让 Discourse 上传文件时,使用

"assets": {
        "fontname": "assets/fontname.woff2"
   }

它似乎将文件上传到了与 assets/roboto.woff2 不同的路径
/uploads/db0202/original/1X/...

编辑:
不需要 $variables,魔法发生在上传过程中。但我之前在 assets 中有 assets/font.svg#regular,而井号(#)造成了问题。

2 个赞

我遇到了同样的错误 Error: Undefined variable。而且,在上传或上传作为标题中也没有显示任何资产。

这个指南需要更新吗?
提前感谢。

1 个赞