-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bab83b6
commit e3ad31e
Showing
4 changed files
with
190 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Gitpod. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import fetch from 'node-fetch'; | ||
import * as vscode from 'vscode'; | ||
import { load } from 'js-yaml'; | ||
|
||
const LAST_READ_RELEASE_DATE = 'gitpod-desktop.releaseNote'; | ||
|
||
export function registerReleaseNoteView(context: vscode.ExtensionContext) { | ||
context.subscriptions.push( | ||
vscode.commands.registerCommand('gitpod.releaseNote', () => { | ||
ReleaseNotePanel.createOrShow(context); | ||
}) | ||
); | ||
|
||
// TODO(hw): Remove | ||
context.subscriptions.push( | ||
vscode.commands.registerCommand('gitpod.cleanReleaseNoteCache', async () => { | ||
await context.globalState.update(LAST_READ_RELEASE_DATE, undefined); | ||
}) | ||
); | ||
|
||
const lastRead = context.globalState.get<string>(LAST_READ_RELEASE_DATE); | ||
shouldShowReleaseNote(lastRead).then(shouldShow => { | ||
if (shouldShow) { | ||
ReleaseNotePanel.createOrShow(context); | ||
} | ||
}); | ||
} | ||
|
||
async function getLastPublish() { | ||
// TODO(hw): fetch from somewhere | ||
return '2022-07-04'; | ||
} | ||
|
||
async function shouldShowReleaseNote(lastRead: string | undefined) { | ||
const date = await getLastPublish(); | ||
console.log(`lastSeen: ${lastRead}, latest publish: ${date} => ${date !== lastRead ? 'show' : 'not-show'} ===============hwen.shouldShow`); | ||
return date !== lastRead; | ||
} | ||
|
||
class ReleaseNotePanel { | ||
public static currentPanel: ReleaseNotePanel | undefined; | ||
public static readonly viewType = 'gitpodReleaseNote'; | ||
private readonly panel: vscode.WebviewPanel; | ||
private lastRead: string | undefined; | ||
private _disposables: vscode.Disposable[] = []; | ||
|
||
private async loadChangelog(date: string) { | ||
// TODO(hw): fetch from somewhere | ||
console.log(date, fetch, 'ignore'); | ||
const resp = await fetch(`https://raw.githubusercontent.com/gitpod-io/website/main/src/lib/contents/changelog/${date}.md`); | ||
if (!resp.ok) { | ||
throw new Error(`Getting GitHub account info failed: ${resp.statusText}`); | ||
} | ||
const md = await resp.text(); | ||
|
||
const parseInfo = (md: string) => { | ||
if (!md.startsWith('---')) { | ||
return; | ||
} | ||
const lines = md.split('\n'); | ||
const end = lines.indexOf('---', 1); | ||
const content = lines.slice(1, end).join('\n'); | ||
return load(content) as { title: string; date: string; image: string; alt: string; excerpt: string }; | ||
}; | ||
const info = parseInfo(md); | ||
|
||
const content = md | ||
.replace(/---.*?---/gms, '') | ||
.replace(/<script>.*?<\/script>/gms, '') | ||
.replace(/<Badge.*?text="(.*?)".*?\/>/gim, '`$1`') | ||
.replace(/<Contributors usernames="(.*?)" \/>/gim, (_, p1) => { | ||
const users = p1 | ||
.split(',') | ||
.map((e: string) => `[${e}](https://github.com/${e})`); | ||
return `Contributors: ${users.join(', ')}`; | ||
}) | ||
.replace(/<p>(.*?)<\/p>/gm, '$1') | ||
.replace(/^[\n]+/m, ''); | ||
if (!info) { | ||
return content; | ||
} | ||
|
||
return [ | ||
`# ${info.title}`, | ||
'> See also https://gitpod.io/changelog', | ||
`![${info.alt ?? 'image'}](https://www.gitpod.io/images/changelog/${info.image})`, | ||
content, | ||
].join('\n\n'); | ||
} | ||
|
||
public async updateHtml(date?: string) { | ||
if (!date) { | ||
date = await getLastPublish(); | ||
} | ||
const mdContent = await this.loadChangelog(date); | ||
const html = await vscode.commands.executeCommand('markdown.api.render', mdContent) as string; | ||
this.panel.webview.html = html; | ||
if (!this.lastRead || date > this.lastRead) { | ||
await this.context.globalState.update(LAST_READ_RELEASE_DATE, date); | ||
this.lastRead = date; | ||
} | ||
} | ||
|
||
public static createOrShow(context: vscode.ExtensionContext) { | ||
const column = vscode.window.activeTextEditor | ||
? vscode.window.activeTextEditor.viewColumn | ||
: undefined; | ||
|
||
if (ReleaseNotePanel.currentPanel) { | ||
ReleaseNotePanel.currentPanel.panel.reveal(column); | ||
return; | ||
} | ||
|
||
const panel = vscode.window.createWebviewPanel( | ||
ReleaseNotePanel.viewType, | ||
'Gitpod Release Note', | ||
column || vscode.ViewColumn.One, | ||
{ enableScripts: true }, | ||
); | ||
|
||
ReleaseNotePanel.currentPanel = new ReleaseNotePanel(context, panel); | ||
} | ||
|
||
public static revive(context: vscode.ExtensionContext, panel: vscode.WebviewPanel) { | ||
ReleaseNotePanel.currentPanel = new ReleaseNotePanel(context, panel); | ||
} | ||
|
||
private constructor( | ||
private readonly context: vscode.ExtensionContext, | ||
panel: vscode.WebviewPanel | ||
) { | ||
this.lastRead = this.context.globalState.get<string>(LAST_READ_RELEASE_DATE); | ||
this.panel = panel; | ||
|
||
this.updateHtml(); | ||
|
||
this.panel.onDidDispose(() => this.dispose(), null, this._disposables); | ||
this.panel.onDidChangeViewState( | ||
() => { | ||
if (this.panel.visible) { | ||
this.updateHtml(); | ||
} | ||
}, | ||
null, | ||
this._disposables | ||
); | ||
} | ||
|
||
public dispose() { | ||
ReleaseNotePanel.currentPanel = undefined; | ||
this.panel.dispose(); | ||
while (this._disposables.length) { | ||
const x = this._disposables.pop(); | ||
if (x) { | ||
x.dispose(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters