-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Setup of Markdown+Mermaid (#209)
- Loading branch information
1 parent
c22544f
commit 89335b5
Showing
7 changed files
with
338 additions
and
170 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,121 @@ | ||
import * as vscode from 'vscode'; | ||
import * as path from 'path'; | ||
import { isAsyncAPIFile } from './PreviewWebPanel'; | ||
import * as Markdownit from 'markdown-it'; | ||
import { Parser, fromFile, AsyncAPIDocumentInterface } from '@asyncapi/parser'; | ||
import Info from './components/Info'; | ||
import MermaidDiagram from './components/MermaidDiagram'; | ||
|
||
const md = Markdownit('commonmark'); | ||
const parser = new Parser(); | ||
|
||
function buildMarkdown(document:AsyncAPIDocumentInterface | undefined){ | ||
|
||
let content = ''; | ||
|
||
if(document !== undefined){ | ||
|
||
content = ` | ||
${Info(document)} | ||
${MermaidDiagram(document)} | ||
`; | ||
} | ||
|
||
return md.render(content); | ||
} | ||
|
||
export function previewMarkdown(context: vscode.ExtensionContext) { | ||
return async (uri: vscode.Uri) => { | ||
uri = uri || (await promptForAsyncapiFile()) as vscode.Uri; | ||
if (uri) { | ||
console.log('Opening asyncapi markdown', uri.fsPath); | ||
openAsyncAPIMarkdown(context, uri); | ||
} | ||
}; | ||
} | ||
|
||
export const openAsyncapiMdFiles: { [id: string]: vscode.WebviewPanel } = {}; // vscode.Uri.fsPath => vscode.WebviewPanel | ||
|
||
|
||
export async function openAsyncAPIMarkdown(context: vscode.ExtensionContext, uri: vscode.Uri) { | ||
const localResourceRoots = [ | ||
vscode.Uri.file(path.dirname(uri.fsPath)), | ||
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/mermaid/dist/mermaid.min.js') | ||
]; | ||
if (vscode.workspace.workspaceFolders) { | ||
vscode.workspace.workspaceFolders.forEach(folder => { | ||
localResourceRoots.push(folder.uri); | ||
}); | ||
} | ||
const panel: vscode.WebviewPanel = | ||
openAsyncapiMdFiles[uri.fsPath] || | ||
vscode.window.createWebviewPanel('markdown-preview', '', vscode.ViewColumn.Two, { | ||
enableScripts: true, | ||
retainContextWhenHidden: true, | ||
localResourceRoots, | ||
}); | ||
|
||
const { document } = await fromFile(parser, uri.fsPath).parse(); | ||
let result = buildMarkdown(document); | ||
|
||
panel.title = path.basename(uri.fsPath); | ||
panel.webview.html = getWebviewContent(context, panel.webview, uri, result); | ||
|
||
panel.onDidDispose(() => { | ||
delete openAsyncapiMdFiles[uri.fsPath]; | ||
}); | ||
openAsyncapiMdFiles[uri.fsPath] = panel; | ||
} | ||
|
||
async function promptForAsyncapiFile() { | ||
if (isAsyncAPIFile(vscode.window.activeTextEditor?.document)) { | ||
return vscode.window.activeTextEditor?.document.uri; | ||
} | ||
const uris = await vscode.window.showOpenDialog({ | ||
canSelectFiles: true, | ||
canSelectFolders: false, | ||
canSelectMany: false, | ||
openLabel: 'Open AsyncAPI file', | ||
filters: { | ||
asyncAPI: ['yml', 'yaml', 'json'], | ||
}, | ||
}); | ||
return uris?.[0]; | ||
} | ||
|
||
function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Webview, asyncapiFile: vscode.Uri, result:any) { | ||
const mermaidJs = webview.asWebviewUri( | ||
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/mermaid/dist/mermaid.min.js') | ||
); | ||
|
||
const html = ` | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<style> | ||
html{ | ||
scroll-behavior: smooth; | ||
} | ||
body { | ||
color: #121212; | ||
background-color: #fff; | ||
word-wrap: break-word; | ||
} | ||
h1 { | ||
color: #121212; | ||
} | ||
</style> | ||
</head> | ||
<body x-timestamp="${Date.now()}"> | ||
${result} | ||
<script src="${mermaidJs}"></script> | ||
<script> | ||
mermaid.initialize({ startOnLoad: true }); | ||
</script> | ||
</body> | ||
</html> | ||
`; | ||
return html; | ||
} |
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,61 @@ | ||
import {AsyncAPIDocumentInterface } from '@asyncapi/parser'; | ||
|
||
export default function info(asyncapi:AsyncAPIDocumentInterface) { | ||
|
||
const info = asyncapi.info(); | ||
const defaultContentType = asyncapi.defaultContentType(); | ||
const specId = info.id(); | ||
const termsOfService = info.termsOfService(); | ||
const license = info.license(); | ||
const contact = info.contact(); | ||
const externalDocs = info.externalDocs(); | ||
const extensions: any = info.extensions(); | ||
|
||
const infoList = []; | ||
if (specId) { | ||
infoList.push(`Specification ID: \`${specId}\``); | ||
} | ||
if (license) { | ||
infoList.push(license.url() ? ( | ||
`License: [${license.name()}](${license.url()})` | ||
) : `License: ${license.name()}`); | ||
} | ||
if (termsOfService) { | ||
infoList.push( | ||
`[${termsOfService}](${termsOfService})` | ||
); | ||
} | ||
if (defaultContentType) { | ||
infoList.push( | ||
`Default content type: [${defaultContentType}](https://www.iana.org/assignments/media-types/${defaultContentType})` | ||
); | ||
} | ||
if (contact) { | ||
if (contact.url()) { | ||
infoList.push( | ||
`Support: [${contact.url()}](${contact.name() || 'Link'})` | ||
); | ||
} | ||
if (contact.email()) { | ||
infoList.push( | ||
`Email support: [${`mailto:${contact.email()}`}](${contact.email()})` | ||
); | ||
} | ||
} | ||
|
||
return ( | ||
` | ||
# ${info.title()} ${info.version()} documentation | ||
${ | ||
infoList.map((value)=>{ | ||
return '\n* '+ value; | ||
}) | ||
} | ||
![${info.title()}](${(extensions.get('x-logo'))?extensions.get('x-logo').value():null}) | ||
#### ${info.description()} | ||
`); | ||
} |
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,7 @@ | ||
import {AsyncAPIDocumentInterface } from '@asyncapi/parser'; | ||
|
||
export default function mermaidDiagram(asyncapi:AsyncAPIDocumentInterface){ | ||
|
||
|
||
return ``; | ||
} |
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