Skip to content

Commit

Permalink
feat(theme): make docs editable on remote Git hosting platform
Browse files Browse the repository at this point in the history
  • Loading branch information
ourai committed Apr 12, 2024
1 parent ecf40bf commit 045d69b
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 129 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ node_modules/
/.knosys/sites/default/themes/*/layout/*
!/.knosys/sites/default/themes/*/layout/_nop/
/.knosys/sites/default/themes/*/scripts/
/.knosys/sites/default/themes/*/source/
/.knosys/sites/default/themes/*/source/fonts/*
/.knosys/sites/default/themes/*/source/images/*
!/.knosys/sites/default/themes/*/source/images/nop/
/.knosys/sites/default/themes/*/source/javascripts/*
/.knosys/sites/default/themes/*/source/stylesheets/*
!/.knosys/sites/default/themes/*/_config.yml
/.knosys/sites/default/public/
/.knosys/sites/default/.deploy*/
Expand Down
36 changes: 33 additions & 3 deletions .knosys/scripts/helper/nop-project.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { resolve: resolvePath } = require('path');
const { existsSync } = require('fs');
const { isArray, isPlainObject, capitalize } = require('@ntks/toolbox');
const { resolveRootPath, getConfig, isDirectory, ensureDirExists, readData, saveData, normalizeFrontMatter } = require('@knosys/sdk');
const { isString, isArray, isPlainObject, capitalize } = require('@ntks/toolbox');
const { resolveRootPath, getConfig, isDirectory, ensureDirExists, readData, readMeta, saveData, normalizeFrontMatter } = require('@knosys/sdk');
const { execute } = require('ksio');

function resolveSiteSrcDir(site) {
Expand Down Expand Up @@ -88,10 +88,28 @@ function resolveCustomizedDocToc(srcPath, items, parentUri, docData) {
return resolved;
}

function resolveRepoSource(sourceUrl) {
return {
host: new URL(sourceUrl).hostname,
url: sourceUrl,
};
}

function resolveRepoData(site, config, existsRepos = {}) {
const rootPath = resolveRootPath();
const siteDataDir = resolvePath(rootPath, `${resolveSiteSrcDir(site)}${config.generator === 'hexo' ? '/source' : ''}/_data`);

let distDataDir;
let distDocDir;

if (config.generator === 'hexo') {
distDataDir = '/source';
distDocDir = 'knosys';
} else {
distDataDir = '';
distDocDir = '_knosys';
}

const siteDataDir = resolvePath(rootPath, `${resolveSiteSrcDir(site)}${distDataDir}/_data`);
const projectRepos = {};

Object.entries(config.data).forEach(([srcKey, srcDir]) => {
Expand All @@ -113,11 +131,23 @@ function resolveRepoData(site, config, existsRepos = {}) {
}

const projectSlug = srcKey.replace(/^project\-/, '');
const source = { local: `${distDocDir}/${srcKey}` };

const { editable } = readMeta(srcPath);

if (editable) {
if (isString(editable)) {
source.remote = { default: resolveRepoSource(editable) };
} else if (isPlainObject(editable)) {
source.remote = Object.entries(editable).reduce((prev, [locale, sourceUrl]) => ({ ...prev, [locale]: resolveRepoSource(sourceUrl) }), {});
}
}

projectRepos[projectSlug] = {
name: `${projectSlug.split('-').map(w => capitalize(w)).join(' ')} 项目文档`,
base: `/projects/${projectSlug}`,
collection: 'docs',
source,
toc,
customized,
};
Expand Down
4 changes: 1 addition & 3 deletions .knosys/sites/default/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ tag_map:

# Metadata elements
## https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta
meta_generator: true
meta_generator: false

# Date / Time format
## Hexo uses Moment.js to parse and display date
Expand All @@ -99,8 +99,6 @@ ignore:
## Themes: https://hexo.io/themes/
theme: nop-project
theme_config:
brand:
# icon: "logo"
header:
navs:
- text: 学习
Expand Down
9 changes: 9 additions & 0 deletions .knosys/sites/default/source/_data/local/repos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ nop-entropy:
name: Nop Entropy 项目文档
base: /projects/nop-entropy
collection: docs
source:
local: knosys/project-nop-entropy
remote:
default:
host: github.com
url: 'https://github.com/entropy-cloud/nop-entropy/edit/master/docs/:path'
zh:
host: gitee.com
url: 'https://gitee.com/canonical-entropy/nop-entropy/edit/master/docs/:path'
toc:
- text: 介绍
slug: why-nop
Expand Down
1 change: 1 addition & 0 deletions .knosys/sites/default/themes/nop-project/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ meta:
url: https://nop-platform.gitee.io
brand:
text: Nop
icon: "images/nop/logo.png"
color: "#0871ab"
copyright:
owner:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,24 @@
<%- partial('_ksio/slots/header') %>
</header>
<div class="Article-content col-md-9"><%- partial('_ksio/slots/content') %></div>
<footer class="Article-footer col-md-3"><%- partial('_ksio/slots/footer') %></footer>
<footer class="Article-footer col-md-3">
<% if (repo && repo.source && repo.source.remote && repo.source.remote.default) { %>
<% const repoSource = repo.source.remote.zh || repo.source.remote.default %>
<% let sourceUrl = repoSource.url %>
<% let sourceText %>
<% if (repoSource.host === 'github.com') { %>
<% sourceText = 'GitHub' %>
<% } else if (repoSource.host === 'gitee.com') { %>
<% sourceText = 'Gitee' %>
<% } %>
<% if (sourceUrl && sourceText) { %>
<div class="Widget">
<div class="Widget-body"><a href="<%= sourceUrl.replace(':path', page.source.replace(`${repo.source.local}/`, '')) %>" target="_blank" rel=" rel="external nofollow""><%= sourceText %> 上编辑文档内容</a></div>
</div>
<% } %>
<% } %>
<%- partial('_ksio/slots/footer') %>
</footer>
</article>
<%- partial(theme.footer && theme.footer.partial || '_ksio/partials/footer') %>
</main>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
124 changes: 10 additions & 114 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nop/website",
"version": "0.0.3",
"version": "0.0.4",
"description": "Official website",
"private": true,
"main": "index.js",
Expand All @@ -18,20 +18,15 @@
},
"dependencies": {
"hexo": "^6.3.0",
"hexo-generator-archive": "^2.0.0",
"hexo-generator-category": "^2.0.0",
"hexo-generator-index": "^3.0.0",
"hexo-generator-tag": "^2.0.0",
"hexo-renderer-coffeescript": "^0.2.0",
"hexo-renderer-ejs": "^2.0.0",
"hexo-renderer-marked": "^6.0.0",
"hexo-renderer-sass": "^0.4.0",
"hexo-renderer-stylus": "^2.1.0",
"hexo-server": "^3.0.0"
},
"devDependencies": {
"@ntks/toolbox": "0.0.6",
"hexo-theme-lime": "0.0.0-alpha.1",
"ksio": "0.0.1"
"ksio": "0.0.2"
}
}

0 comments on commit 045d69b

Please sign in to comment.