从零开始掘金markdown主题 - 妹抖龙

3,671 阅读6分钟

小知识,大挑战!本文正在参与「程序员必备小知识」创作活动

本文已参与「掘力星计划」,赢取创作大礼包,挑战创作激励金。

这几天升级 Win11,从零开始有了N多的出场机会。妹抖龙2 前两周也完结了,趁着这波机会,下面我们将从零开始创建一个妹抖龙掘金主题,在有限的时间里尽量还原更多的细节,主题是真正的从零开始哦。

第一步,前置条件准备

要整一个掘金主题,这里我们需要的前置条件有:

一个正常的 github 帐号

这里我直接就ok了,没有的同学自己可以打开 Github 自己注册一个,过程就比较简单,我们直接进行下一步

版本控制工具 Git

我们可以通过到 Git 官网,直接点击下载自己对应的平台架构直接安装即可

image.png

代码工具

代码工具流行的有WebStorm/IntelliJ IDEA 或者 Visual Studio Code,这里我们使用 IntelliJ IDEA 来进行开发,我们可以打开 IntelliJ IDEA 官网,选择下载直接进行安装即可

image.png

这里我推荐另外一种方式,使用 Toolbox App 进行下载安装,这个 Toolbox App 非常的好用,可以直接下载管理所有 JetBrains 相关的开发工具

image.png

这里我们已经下载了 Android StudioIntelliJ IDEA ,如果想要安装其他应用,直接点击安装即可,管理特别的方便,还支持回滚版本,其他功能自行体验

这里我们打开 Nodejs 官网,看个人爱好,选择 LTS 版本还是最新版本

image.png

这里我同样推荐使用一个专门Node的管理工具 NVM-windows 来管理Node版本,我们到下载页 releases 下载最新版本并且安装即可,我这里已经下载安装好了

image.png

这里我需要安装最新的 LTS 长期支持版本,以管理员身份运行 Windows Terminal ,接着我们执行 nvm install lts ,这将会自动安装最新的 LTS Node版本,以及最新的 NPM

nvm install lts

image.png

如果要安装最新版本,可以执行nvm install latest,更多命令,请查看 nvm-windows 文档

Node装好了,我比较喜欢使用 yarn 来进行包管理, 执行 npm install -g yarn 安装 yarn,也可以在 官网 进行下载。点击下载 最新版本 直接安装即可。

第二步,Coding

克隆项目

我们打开 掘金主题模板 ,点击 Use this template 按钮,

image.png

这里需要填写我们的仓库名称,以及其他相关信息,填写好了之后,我们点击 Create repository from template 创建。

image.png

我已经提前创建好了 juejin-markdown-theme-maidragon 仓库,复制链接

image.png

打开我们的 IntelliJ IDEA ,点击 Get from VCS

image.png

填写好我们的刚刚粘贴的地址,选择好项目位置和名称

image.png

点击 Clone 完成下载。项目打开完成,结构如下

image.png

开始上手

我们先将 juejin.scss 重命名为 maidragon.scss ,接着编辑一下 package.json 这个文件,把内容换成自己的,并添加一条 preview 脚本,方便自己调试,juejin-theme-devtool 是掘金的主题开发工具,详情可以自己打开查看,实际有用的就下面的 npx juejin-theme-devtool maidragon.scss

{
  "name": "juejin-markdown-theme-maidragon",
  "author": "iota9star",
  "homepage": "https://github.com/iota9star/juejin-markdown-theme-maidragon",
  "private": true,
  "license": "MIT",
  "scripts": {
    "preview": "npx juejin-theme-devtool maidragon.scss"
  },
  "dependencies": {
    "bytemd": "^1.2.0"
  }
}

现在我们执行 yarn install ,等待包安装完毕。

image.png

点击运行按钮,先执行看下效果

image.png

打开链接,我们先来看看原版的效果

image.png

干干净净,没有一丝的花哨,现在我们打开 妹抖龙官网 ,找找主题色等等

image.png

大概得到这些,现在从上到下开始设置样式。我们先来整个背景

.markdown-body {
  word-break: break-word;
  line-height: 1.75;
  font-weight: 400;
  font-size: 15px;
  overflow-x: hidden;
  color: $-font-color;
  background-image: url($-bg);
  background-size: 100% auto;
  background-repeat: repeat;
}

image.png

这样看起来有点傻,我们再加两句

margin: 0 -2rem;
padding: 0 2rem;

image.png

这样看起来就不错了,现在给 H1 加点魔法

h1 {
  color: $-green;
  font-size: 30px;
  margin-bottom: 5px;
  align-self: center;
  position: relative;
  z-index: 0;

  &:before, &:after {
    content: "";
    position: absolute;
    background-repeat: no-repeat;
    background-size: contain;
  }

  &:before {
    top: -24px;
    left: -36px;
    width: 96px;
    height: 96px;
    background-image: url($-hat);
    transform: rotate(-36deg);
  }

  &:after {
    top: -36px;
    right: -78px;
    width: 78px;
    height: 78px;
    background-image: url($-dragon);
    z-index: -1;
  }
}

image.png

现在给剩下的 h2 h3 h4 h5 h6 加上魔法,太复杂会有点丑,我们还是搞的简单一点

h2,
h3,
h4,
h5,
h6 {
  &:before {
    display: inline-block;
    background-color: rgba($-orange, .87);
    z-index: -1;
    transition: border-radius .3s;
  }

  &:hover {
    &:before {
      border-radius: 50%;
    }

    &:after {
      animation: shakeit 0.3s;
    }
  }
}

h2 {
  font-size: 24px;
  border-bottom: 4px solid rgba($-green, .36);
  padding-bottom: 12px;

  &:before {
    width: 16px;
    height: 16px;
    left: -4px;
  }

  &:after {
    bottom: 0;
    right: 0;
    width: 36px;
    height: 56px;
    background-image: url($-dragon);
    z-index: -1;
  }
}

h3 {
  font-size: 18px;
  padding-bottom: 0;

  &:before {
    width: 14px;
    height: 14px;
    left: -4px;
  }
}

h4 {
  font-size: 16px;

  &:before {
    width: 12px;
    height: 12px;
    left: -4px;
  }
}

h5 {
  font-size: 15px;

  &:before {
    width: 10px;
    height: 10px;
    left: -4px;
  }
}

h6 {
  margin-top: 5px;

  &:before {
    width: 8px;
    height: 8px;
    left: -4px;
  }
}

image.png

下一个添加魔法的是 段落 ,文字没有啥好说的

text-align: justify;
color: $-font-color;

image.png

下一个是 表格 ,直接看效果

image.png

table {
  font-size: 13px;
  width: 100%;
  max-width: 100%;
  overflow: auto;
  border: solid 4px rgba($-green, 0.38);
  border-collapse: collapse;
  position: relative;
  z-index: 1;
  margin-top: 48px;

  &:after {
    width: 100%;
    height: calc(100% + 48px);
    background-image: url($-dragon);
    background-position: right;
    z-index: -1;
    content: "";
    bottom: 0;
    right: -12px;
    position: absolute;
    background-repeat: no-repeat;
    background-size: contain;
    opacity: 0.24;
  }

  thead {
    background: rgba($-green, 0.2);
    color: $-font-color;
    text-align: left;
  }

  tr:nth-child(2n) {
    background-color: rgba($-green, 0.1);
  }

  th,
  td {
    padding: 12px 16px;
    line-height: 24px;
  }

  td {
    min-width: 100px;
  }
}

下面给 各种 字符 行内代码 加上效果

del {
  text-decoration: none;
  position: relative;
  z-index: 1;

  &:before {
    content: "";
    left: -4px;
    right: -4px;
    height: 6px;
    background-color: rgba($-orange, .48);
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: -1;
    border-radius: 2px;
  }
  &:hover{
    &:before{
      background-color: rgba($-orange, .96);
    }
  }
}

code {
  font-family: $monospace-font;
  word-break: break-word;
  border-radius: 2px;
  overflow-x: auto;
  background-color: rgba($-green, .2);
  color: $-dark-green;
  font-size: 0.72em;
  padding: 0.2em 0.48em;
  margin: 0 0.48em;
  line-height: 1.75;
}

image.png

图片 安排上,简简单单居中

img {
  max-width: 100%;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}

image.png

下一个是 列表 ,调整了一下样式,简简单单处理一下

ol,
ul {
  padding-left: 2rem;

  li {
    margin-bottom: 0;
    list-style: inherit;
    position: relative;
    z-index: 1;

    &::marker {
      color: $-green;
    }

    input[type="checkbox"] {
      background-color: transparent !important;
      color: transparent !important;
      border: none !important;
      appearance: none;
      margin: 0;
      padding: 0;
      box-sizing: border-box;

      &:before {
        content: "";
        position: absolute;
        width: 10px;
        height: 10px;
        border-radius: 50%;
        left: 0;
        top: 5px;
        border: 2px solid rgba($-orange, .87);
      }

      &:checked:after {
        content: "";
        border-left: 2px solid white;
        border-bottom: 2px solid white;
        z-index: 1;
        width: 6px;
        height: 3px;
        left: 3px;
        top: 11px;
        position: absolute;
        transform: translateY(-50%) rotate(-45deg);
      }

      &:checked:before {
        background-color: $-green;
        border-color: $-green;
      }
    }

    &.task-list-item {
      list-style: none;
      padding-left: 20px;

      ul,
      ol {
        margin-top: 0;
      }
    }
  }

  ul,
  ol {
    margin-top: 3px;
  }
}

ol > li {
  &::marker {
    color: $-green;
    font-weight: bold;
    font-style: italic;
  }
}

ol li {
  padding-left: 6px;
}

image.png

image.png

image.png

下一个是 链接

a {
  text-decoration: none;
  color: $-green;
  background-color: rgba(255, 255, 255, .9);
  position: relative;
  font-weight: bold;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all .3s;
  font-size: 13px;
  margin: 0 6px;
  z-index: 0;

  &:before, &:after {
    position: absolute;
    content: "";
    width: 0;
    height: 0;
    border-radius: 50%;
  }

  &:before {
    right: 0;
    top: 0;
    transition: all .4s;
    z-index: 1;
  }

  &:after {
    left: 0;
    bottom: 0;
    transition: all .3s;
    z-index: -2;
  }

  &:hover,
  &:active {
    color: white;
    background-color: $-green;
    border-bottom-left-radius: 12px;
    border-top-right-radius: 12px;

    &:before {
      top: -4px;
      right: -4px;
      width: 8px;
      height: 8px;
      background-color: rgba($-red, .87);
    }

    &:after {
      bottom: -4px;
      left: -6px;
      width: 12px;
      height: 12px;
      background-color: rgba($-orange, .87);
    }
  }
}

image.png

最后还有个 引用

blockquote {
  color: #444;
  margin: 22px 0;
  padding: 1.6rem;
  background-color: rgba($-green, .1);
  position: relative;
  border-radius: 36px 16px 16px 16px;
  transition: .2s cubic-bezier(.18, .89, .52, 1.94);

  &:hover {
    transform: scale(1.05) rotate(3deg);
  }

  &:before {
    position: absolute;
    content: "";
    width: 36px;
    height: 36px;
    background-image: url($-hover);
    z-index: 1;
    left: -8px;
    top: -8px;
  }

  &::after {
    display: block;
    content: '”';
    font-weight: bold;
    font-size: 56px;
    height: 56px;
    color: $-green;
    position: absolute;
    right: -16px;
    bottom: 0;
  }

  & > p {
    margin: 10px 0;
  }
}

image.png

现在大致就ok了,看一眼全部效果,有些动效截图是截不了的,时间问题,先这样,我们先提交一波代码

screenshot-localhost_3000-2021.10.08-23_29_45.png

掘金主题 PR

我们先 fork 一手 juejin-markdown-themes

image.png

fork 成功以后,我们像先前一下,clone 到本地

image.png

image.png

我们执行 git checkout -b maidragon 创建一个 maidragon 的分支,按照文档修改相关内容

image.png

在 themes.js 中填写妹抖龙主题相关的信息,这里的 ref 我们可以通过执行 git branch -vv 查看

maidragon: {
  owner: 'iota9star',
  repo: 'juejin-markdown-theme-maidragon',
  path: 'maidragon.scss',
  ref: '10d038a'
}

最后在 README.md 中添加上自己的信息,提交代码

image.png

下面我们打开我们的仓库 juejin-markdown-themes ,点击 Compare & pull request,创建一个 PR

image.png

这里我们没有什么需要修改的,直接点击 Create pull request 提交即可

image.png

我们现在只需要等待审核就OK啦

image.png

总结

  • 贡献主题还是蛮简单的,操作的步骤也挺少的。希望后面有越来越多的人贡献主题

  • 但愿我这个主题可以过审,主题感觉还是不错的,比较清爽。

  • 我们可以 在这儿 预览一下效果,只需要把主题换成 maidragon 即可在线预览

image.png

如果文章对您有帮助的话,欢迎 点赞 、 评论 、 关注 、 收藏 、 分享 ,您的支持是我码字的动力,万分感谢!!!🌈

参考地址