Skip to content

Commit

Permalink
add enhancement to the code and add the export to MD feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilkalburgi committed Jul 7, 2024
1 parent 04b756b commit 3f024b5
Show file tree
Hide file tree
Showing 16 changed files with 878 additions and 602 deletions.
20 changes: 20 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@
"dependencies": {
"@asyncapi/parser": "^3.0.7",
"@types/markdown-it": "^13.0.7",
"@types/turndown": "^5.0.4",
"ejs": "^3.1.9",
"markdown-it": "^14.0.0",
"mermaid": "^10.8.0",
"panzoom": "^9.4.3",
"turndown": "^7.2.0",
"yaml": "^2.4.2"
}
}
1,077 changes: 630 additions & 447 deletions src/Asyncapi.ts

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/Diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default async function diagnosticsMarkdown(diagnostics: ISpectralDiagnost
let joinedPath: string = "";
diagnostics.forEach(diagnostic =>{
joinedPath = diagnostic.path.join(' / ');
console.log(joinedPath, recentErrorPath, data.length);
if(joinedPath.indexOf(recentErrorPath) === -1 || !recentErrorPath){
recentErrorPath = joinedPath;
data.push({
Expand Down
4 changes: 4 additions & 0 deletions src/Flowchart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export default async function flowchart(asyncapi: any, context: vscode.Extension
});
}
});
channelInfo.servers?.map((server: any)=>{
if(typeof server === "string")
{data.relations.push(`${channelName} --> ${server.replace(/~1/gi,"/")}`);}
});
}
});

Expand Down
94 changes: 55 additions & 39 deletions src/PreviewMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,47 @@ import ClassDiagram from './ClassDiagram';
const parser = new Parser();
parser.registerSchemaParser(AvroSchemaParser());

function parsedAsyncapiPreview(){

const editor: any = vscode.window.activeTextEditor;
if(!editor) {return;}
const document = editor.document;
const filePath: any = document?.fileName;
const fullPath = path.resolve(filePath);
const content = fs.readFileSync(fullPath, 'utf8');


let parsedData;
if (filePath.endsWith('.json')) {
parsedData = JSON.parse(content);
} else if (filePath.endsWith('.yaml') || filePath.endsWith('.yml')) {
parsedData = parse(content);
} else {
vscode.window.showInformationMessage('Unsupported file type.');
return;
}
return parsedData || "";
function parsedAsyncapiPreview(){
const editor: any = vscode.window.activeTextEditor;
if(!editor) {return;}
const document = editor.document;
const filePath: any = document?.fileName;
const fullPath = path.resolve(filePath);
const content = fs.readFileSync(fullPath, 'utf8');
let parsedData;
if (filePath.endsWith('.json')) {
parsedData = JSON.parse(content);
} else if (filePath.endsWith('.yaml') || filePath.endsWith('.yml')) {
parsedData = parse(content);
} else {
vscode.window.showInformationMessage('Unsupported file type.');
return;
}
return parsedData || "";
}



async function buildMarkdown(document: any, diagnostics: ISpectralDiagnostic[], context: vscode.ExtensionContext){


let content = '';

let content: any = '';
if(document !== undefined){
vscode.window.onDidChangeActiveTextEditor(()=>null);
document.isAsyncapiParser = true;
content = await Asyncapi(document, context);
content = await Asyncapi(document, context) || "";
}else{
content = await Diagnostics(diagnostics, context);
vscode.window.onDidChangeActiveTextEditor(async()=>{

let parsedData: any = parsedAsyncapiPreview();
if(parsedData){
parsedData.isAsyncapiParser = false;
content += await Asyncapi(parsedData, context);
}
});
let parsedData: any = parsedAsyncapiPreview();
if(parsedData){
parsedData.isAsyncapiParser = false;
content += await Asyncapi(parsedData, context);
content = await Diagnostics(diagnostics, context);
content += await Asyncapi(parsedData, context) || "";
}
}

return content;

}
Expand Down Expand Up @@ -149,6 +140,9 @@ function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Web
const panzoomJs = webview.asWebviewUri(
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/panzoom/dist/panzoom.min.js')
);
const turndownJs = webview.asWebviewUri(
vscode.Uri.joinPath(context.extensionUri, 'dist/node_modules/turndown/dist/turndown.js')
);
const globalsCSS = webview.asWebviewUri(
vscode.Uri.joinPath(context.extensionUri, 'dist/globals.css')
);
Expand Down Expand Up @@ -187,16 +181,20 @@ function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Web
<button class="button" onclick="moveToSection('section3')">Class Diagram</button>
</div>
<div id="copy-button"><b>Copy</b></div>
<div id="copied-indicator">Copied!</div>
<script src="${mermaidJs}"></script>
<script src="${panzoomJs}"></script>
<script src="${turndownJs}"></script>
<script>
let sectionElement;
let zoomLevelSpan = document.getElementById('flowchart_zoom-level');
let scrollPositionSpan = document.getElementById('flowchart_scroll-position');
const controlPanel = document.getElementById('control-panel');
const zoomInfo = document.getElementById('zoom-info');
var turndownService = new TurndownService();
turndownService.keep(['table','tr','th','td']);
mermaid.initialize({
"startOnLoad": true ,
Expand Down Expand Up @@ -321,11 +319,29 @@ function getWebviewContent(context: vscode.ExtensionContext, webview: vscode.Web
const { x, y } = panZoomClassDiagramInstance.getTransform()
panZoomClassDiagramInstance.smoothMoveTo(x+50, y);
}
});
});
});
document.getElementById('copy-button').addEventListener('click', function() {
const textToCopy = turndownService.turndown(document.getElementById('turndown'));
navigator.clipboard.writeText(textToCopy).then(function() {
showCopiedIndicator()
}).catch(function(error) {
console.error('Failed to copy text: ', error);
});
});
function showCopiedIndicator() {
const indicator = document.getElementById('copied-indicator');
indicator.style.top = "10px";
setTimeout(()=>{
indicator.style.top = "-50px";
},2000);
console.log("reomved");
}
window.addEventListener("resize",()=>{
sectionElement.scrollIntoView({ behavior: 'smooth' });
});
Expand Down
8 changes: 5 additions & 3 deletions src/components/Asyncapi.ejs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<%- include(path.infoPath,{...info,tagsPath: path.tagsPath}) %>
<%- include(path.serversPath,{...servers, ...path}) %>
<%- include(path.operationsPath,{...operations, ...path}) %>
<div id="turndown">
<%- include(path.infoPath,{...info,tagsPath: path.tagsPath}) %>
<%- include(path.serversPath,{...servers, ...path}) %>
<%- include(path.operationsPath,{...operations, ...path}) %>
</div>
14 changes: 7 additions & 7 deletions src/components/Info.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@
<ul>
<% if (specId) { %>
<li>
Specification ID: <%= specId %>
<b>Specification ID:</b> <%= specId %>
</li>
<% } %>
<% if (license) { %>
<% if (license.url()) { %>
<li>
License: <a href="<%= license.url() %>"> <%= license.name() %> </a>
<b>License:</b> <a href="<%= license.url() %>"> <%= license.name() %> </a>
</li>
<% }else { %>
<li>
License: <a href="#"> <%= license.name() %> </a>
<b>License:</b> <a href="#"> <%= license.name() %> </a>
</li>
<% } %>
<% } %>
<% if (termsOfService) { %>
<li>
Terms of service: <%= termsOfService %>
<b>Terms of service:</b> <%= termsOfService %>
</li>
<% } %>
<% if (defaultContentType) { %>
<li>
Default content type: <a href="https://www.iana.org/assignments/media-types/<%= defaultContentType %>"> <%= defaultContentType %> </a>
<b>Default content type:</b> <a href="https://www.iana.org/assignments/media-types/<%= defaultContentType %>"> <%= defaultContentType %> </a>
</li>
<% } %>
<% if (contact) { %>
<% if (contact.url()) { %>
<li>
Support: <a href="<%= contact.url() %>"> <%= contact.name() || 'Link' %> </a>
<b>Support:</b> <a href="<%= contact.url() %>"> <%= contact.name() || 'Link' %> </a>
</li>
<% } %>
<% if (contact.email()) { %>
<li>
Email support: <a href="<%= contact.email() %>"> <%= contact.email() %> </a>
<b>Email support:</b> <a href="<%= contact.email() %>"> <%= contact.email() %> </a>
</li>
<% } %>
<% } %>
Expand Down
Loading

0 comments on commit 3f024b5

Please sign in to comment.