Skip to content

Commit a2d8017

Browse files
committed
feat(sitemap): Auto generate sitemap if remote domain config exist.
1 parent d457811 commit a2d8017

File tree

8 files changed

+100
-7
lines changed

8 files changed

+100
-7
lines changed

src/baseTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export interface OnedriveConfig {
7272
export interface InvioPluginSettings {
7373
s3: S3Config;
7474
password: string;
75+
remoteDomain: string;
7576
serviceType: SUPPORTED_SERVICES_TYPE;
7677
currLogLevel?: string;
7778
autoRunEveryMilliseconds?: number;

src/exporter.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,16 @@ export const publishFiles = async (
116116
log.info('download list: ', externalFiles);
117117
}
118118
}
119-
119+
if (settings.remoteDomain) {
120+
const sitemapDomStr = HTMLGenerator.generateSitemap(allFiles, settings.remoteDomain);
121+
const sitemapDownload = new Downloadable('sitemap.xml', sitemapDomStr, htmlPath.joinString(settings.localWatchDir));
122+
Object.assign(sitemapDownload, {
123+
path: `${sitemapDownload.relativeDownloadPath.asString}/${sitemapDownload.filename}`,
124+
key: settings.localWatchDir + '/' + sitemapDownload.filename,
125+
})
126+
log.info('sitemap - ', sitemapDomStr, htmlPath);
127+
externalFiles.push(sitemapDownload);
128+
}
120129
externalFiles = externalFiles.filter((file, index) => externalFiles.findIndex((f) => f.relativeDownloadPath == file.relativeDownloadPath && f.filename === file.filename) == index);
121130
await Utils.downloadFiles(externalFiles, htmlPath, view);
122131
log.info('download files to: ', htmlPath, externalFiles);

src/html-generation/html-generator.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ export class HTMLGenerator {
9696
// leftSidebar.appendChild(fileTree);
9797
const dataNode = this.generateRootDirNode(file, usingDocument);
9898
leftSidebar.appendChild(dataNode);
99-
const rootDir = file.exportPath.directory.asString?.split('/')[0];
100-
101-
file.downloads.push(new Downloadable('_common-left-tree.html', fileTree.outerHTML, new Path(file.exportPath.asString.split('/')[0])));
99+
const rootDir = new Path(file.exportPath.asString.split('/')[0])
100+
file.downloads.push(new Downloadable('_common-left-tree.html', fileTree.outerHTML, rootDir));
102101
}
103102

104103
await this.appendFooter(file);
@@ -657,6 +656,41 @@ export class HTMLGenerator {
657656
return treeItems;
658657
}
659658

659+
private static createXMLNode(file: TFile, usingDocument: Document, domain: string) {
660+
const url = usingDocument.createElement("url");
661+
662+
const loc = usingDocument.createElement('loc');
663+
loc.innerHTML = Utils.getRemoteUrl(file, domain);
664+
665+
const lastmod = usingDocument.createElement("lastmod");
666+
lastmod.innerHTML = new Date(file.stat.mtime).toISOString().slice(0, 10);
667+
668+
url.appendChild(loc);
669+
url.appendChild(lastmod);
670+
return url;
671+
}
672+
673+
static generateSitemap(fileList: TFile[], domain: string): string {
674+
var doc = document.implementation.createDocument('', '', null);
675+
676+
//create the outer tag
677+
var urlset = doc.createElement("urlset");
678+
urlset.setAttribute("xmlns","http://www.sitemaps.org/schemas/sitemap/0.9");
679+
680+
//first create static sites (note, that this is a selection)
681+
for (var i=0; i < fileList.length; i++) {
682+
const target = fileList[i];
683+
if (target instanceof TFile) {
684+
const urlItemNode = this.createXMLNode(target, doc, domain);
685+
urlset.appendChild(urlItemNode);
686+
}
687+
}
688+
doc.appendChild(urlset);
689+
var oSerializer = new XMLSerializer();
690+
var xmltext = oSerializer.serializeToString(doc);
691+
return `<?xml version="1.0" encoding="UTF-8"?>${xmltext}`;
692+
}
693+
660694
private static generateBrandHeader(usingDocument: Document, brand: string, icon: string, slogan: string, homeLink: string) {
661695
// site-body-left-column-site-name
662696
/**

src/langs/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
"modal_sizesconflict_copynotice": "All the sizes conflicts info have been copied to the clipboard!",
9595

9696
"settings_basic": "Basic Settings",
97+
"settings_domain": "Domain",
98+
"settings_domain_desc": "The option to point a domain to your S3 (or other COS) is optional, but recommended for previewing and generating a website that is SEO-friendly. \n Like https://search.google.com",
99+
"settings_domain_saved": "Domain saved",
97100
"settings_password": "Encryption Password",
98101
"settings_password_desc": "Password for E2E encryption. Empty for no password. You need to click \"Confirm\". Attention: the password and other info are saved locally.",
99102
"settings_autorun": "Schedule For Auto Run",

src/langs/zh_cn.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
"modal_sizesconflict_copynotice": "所有的文件大小冲突信息,已被复制到剪贴板!",
9595

9696
"settings_basic": "基本设置",
97+
"settings_domain": "域名",
98+
"settings_domain_desc": "(可选)绑定的指向S3(或其他COS)的域名,主要用于预览和部署网页时SEO相关配置的自动注入。\n例如:https://search.google.com",
99+
"settings_domain_saved": "域名保存成功",
97100
"settings_password": "密码",
98101
"settings_password_desc": "端到端加密的密码。不填写则代表没密码。您需要点击“确认”来修改。注意:密码和其它信息都会在本地保存。",
99102
"settings_autorun": "自动运行",

src/langs/zh_tw.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
"modal_sizesconflict_copynotice": "所有的檔案大小衝突資訊,已被複制到剪貼簿!",
9595

9696
"settings_basic": "基本設定",
97+
"settings_domain": "域名",
98+
"settings_domain_desc": "(可选)绑定的指向S3(或其他COS)的域名,主要用于预览和部署网页时SEO相关配置的自动注入。\n例如:https://search.google.com",
99+
"settings_domain_saved": "域名保存成功",
97100
"settings_password": "密碼",
98101
"settings_password_desc": "端到端加密的密碼。不填寫則代表沒密碼。您需要點選“確認”來修改。注意:密碼和其它資訊都會在本地儲存。",
99102
"settings_autorun": "自動執行",

src/main.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ import { Path } from './utils/path';
6262
import { HTMLGenerator } from './html-generation/html-generator';
6363
import icon, { UsingIconNames, getIconSvg, addIconForconflictFile } from './utils/icon';
6464
import { StatsView, VIEW_TYPE_STATS, LogType } from "./statsView";
65+
import { DomaindModal } from './remoteDomainModal';
6566

6667
const { iconNameSyncWait, iconNameSyncPending, iconNameSyncRunning, iconNameLogs, iconNameSyncLogo } = UsingIconNames;
6768

6869
const DEFAULT_SETTINGS: InvioPluginSettings = {
6970
s3: DEFAULT_S3_CONFIG,
7071
password: "",
72+
remoteDomain: '',
7173
serviceType: "s3",
7274
currLogLevel: "info",
7375
// vaultRandomID: "", // deprecated
@@ -114,6 +116,21 @@ export default class InvioPlugin extends Plugin {
114116
return false;
115117
}
116118

119+
async checkDomain() {
120+
// Domain check
121+
return new Promise((resolve) => {
122+
if (this.settings?.remoteDomain) {
123+
resolve(this.settings.remoteDomain);
124+
}
125+
if (!this.settings?.remoteDomain) {
126+
// Show modal to get
127+
new DomaindModal(this.app, this, this.settings?.remoteDomain, (newDomain) => {
128+
resolve(newDomain);
129+
}).open()
130+
}
131+
})
132+
}
133+
117134
async syncRun(triggerSource: SyncTriggerSourceType = "manual", fileList?: string[]) {
118135
const t = (x: TransItemType, vars?: any) => {
119136
return this.i18n.t(x, vars);
@@ -309,8 +326,10 @@ export default class InvioPlugin extends Plugin {
309326
log.info('plan.mixedStates: ', plan.mixedStates, touchedFileMap); // for debugging
310327

311328
try {
312-
loadingModal.close();
313-
loadingModal = null;
329+
if (loadingModal) {
330+
loadingModal.close();
331+
loadingModal = null;
332+
}
314333

315334
if (triggerSource !== 'force') {
316335
await new Promise((resolve, reject) => {
@@ -556,7 +575,7 @@ export default class InvioPlugin extends Plugin {
556575
triggerSource: triggerSource,
557576
syncStatus: this.syncStatus,
558577
});
559-
loadingModal.close();
578+
loadingModal?.close();
560579
log.error(msg);
561580
log.error(error);
562581
getNotice(null, msg, 10 * 1000);

src/settings.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,27 @@ export class InvioSettingTab extends PluginSettingTab {
520520
})
521521
);
522522

523+
let remoteDomain = `${this.plugin.settings.remoteDomain}`;
524+
new Setting(s3Div)
525+
.setName(t("settings_domain"))
526+
.setDesc(t("settings_domain_desc"))
527+
.addText((text) => {
528+
text
529+
.setPlaceholder("https://docs.google.com")
530+
.setValue(`${this.plugin.settings.remoteDomain || ''}`)
531+
.onChange(async (value) => {
532+
remoteDomain = value.trim();
533+
});
534+
})
535+
.addButton(async (button) => {
536+
button.setButtonText(t("confirm"));
537+
button.onClick(async () => {
538+
this.plugin.settings.remoteDomain = remoteDomain
539+
await this.plugin.saveSettings();
540+
log.info('new domain: ', t("settings_domain_saved") + " " + remoteDomain)
541+
new Notice(t("settings_domain_saved") + " " + remoteDomain)
542+
});
543+
});
523544
// if (VALID_REQURL) {
524545
// new Setting(s3Div)
525546
// .setName(t("settings_s3_bypasscorslocally"))

0 commit comments

Comments
 (0)