Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: switch-to-jsx-renderer #316

Closed
wants to merge 10 commits into from

Conversation

ritik307
Copy link

Description

  • Switched from template/index.html to template/index.js

Related issue(s)

Resolves #185

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@ritik307
Copy link
Author

@magicmatatjahu I have made index.js as requested kindly review the PR.😁

Copy link
Member

@magicmatatjahu magicmatatjahu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ritik307 Hello! I made review. Good start, but we need some things to improve :)

Please, don't be horrified by these comments :) I gave hints what you should improve. Remember an important thing: the new template should work similarly (create the same file) as the previous template (written in Nunjucks). You can generate an old template and modify the new one to get the same result to check that everything is ok. It actually has to work the same, otherwise we will not be able to merge it.

components/common.js Outdated Show resolved Hide resolved
components/common.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
@ritik307
Copy link
Author

ritik307 commented Jan 26, 2022

Hi @magicmatatjahu I have implemented the changes you have mentioned but I am confused about how to test them :(

Copy link
Member

@magicmatatjahu magicmatatjahu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template/index.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
@ritik307
Copy link
Author

ritik307 commented Feb 3, 2022

@magicmatatjahu I ma made the requested changes kindly look at them :)

Copy link
Member

@magicmatatjahu magicmatatjahu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ritik307 Better, but we still need to improve few things :) Awesome progress 🚀

components/common.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
components/common.js Outdated Show resolved Hide resolved
components/common.js Outdated Show resolved Hide resolved
components/common.js Outdated Show resolved Hide resolved
@ritik307
Copy link
Author

ritik307 commented Feb 9, 2022

@magicmatatjahu Made the requested changes kindly look into them :)

Comment on lines 17 to 28
const links = cssLinks
.map((link) => `<link rel="stylesheet" href="${link}">\n`);

const content = `
<head>
<meta charset="utf-8">
${base && `<base href=${base}>`}
<title>${title}</title>
${withIndendation(links, 2, IndentationTypes.SPACES)}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
${(styleContent.length > 0) ? `<style>\n${styleContent[0]}\n${styleContent[1]}</style>` : ""}
${(cssLinks.length > 0) ? `\n<link href=${cssLinks[0]}>\n<link href=${cssLinks[1]}>\n` : ""}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have performed links and saved in the links array. Why you do it again in the 28 line? :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically, we have cssLinks array and styleContent array so if I am thinking correctly we will only add styleContent when the array is not empty and the same goes for cssLinks so according to me we have to check them separately. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you understand it correctly, but please remove:

  const links = cssLinks
    .map((link) => `<link rel="stylesheet" href="${link}">\n`);

because currently it's unnecessary :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! :)

components/common.js Outdated Show resolved Hide resolved
@magicmatatjahu
Copy link
Member

magicmatatjahu commented Feb 9, 2022

@ritik307 Could you fix https://sonarcloud.io/project/issues?id=asyncapi_html-template&pullRequest=316&resolved=false&types=CODE_SMELL problems? :)

You should move that interpolation strings to outside as save in variables and then reuse them in final string.

@ritik307
Copy link
Author

@magicmatatjahu Made the changes. Kindly review :)

Copy link
Member

@magicmatatjahu magicmatatjahu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall good :) We need to fix some things because we have broken template. Also I wrote about missmatch between ESM and CJS modules in filters/all file, so please change content of that file to:

import fs from 'fs';
import path from 'path';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import AsyncApiComponent, { hljs } from '@asyncapi/react-component';
import { AsyncAPIDocument } from '@asyncapi/parser';

/**
 * Prepares configuration for component.
 */
function prepareConfiguration(params = {}) {
  const config = { show: { sidebar: true }, sidebar: { showOperations: 'byDefault' } };
  if (params.sidebarOrganization === 'byTags') {
    config.sidebar.showOperations = 'bySpecTags';
  }
  if (params.sidebarOrganization === 'byTagsNoRoot') {
    config.sidebar.showOperations = 'byOperationsTags';
  }
  return config;
}

let initLanguages = false;
/**
 * Load all language configurations from highlight.js
 */
function loadLanguagesConfig() {
  if (initLanguages === true) {
    return;
  }

  /**
   * Retrieve the location of highlight.js.
   * It's needed because someone can have installed `highlight.js` as global dependency
   * or depper than local `node_modules` of this template.
   */
  const hljsPackageDir = path.dirname(require.resolve("highlight.js/package.json"))
  const hljsLanguagesPath = path.resolve(hljsPackageDir, 'lib/languages');
  const languages = fs.readdirSync(hljsLanguagesPath);

  for (let langPath of languages) {
    const lang = require(path.resolve(hljsLanguagesPath, langPath.replace('.js', '')));
    hljs.registerLanguage(lang.name, lang);
  }

  initLanguages = true;
}

/**
 * More safe function to include content of given file than default Nunjuck's `include`.
 * Attaches raw file's content instead of executing it - problem with some attached files in template.
 */
export function includeFile(pathFile) {
  const pathToFile = path.resolve(__dirname, '../', pathFile);
  return fs.readFileSync(pathToFile);
}

/**
 * Stringifies the specification with escaping circular refs 
 * and annotates that specification is parsed.
 */
export function stringifySpec(asyncapi) {
  return AsyncAPIDocument.stringify(asyncapi);
}

/**
 * Stringifies prepared configuration for component.
 */
export function stringifyConfiguration(params) {
  return JSON.stringify(prepareConfiguration(params));
}

/**
 * Renders AsyncApi component by given AsyncAPI spec and with corresponding template configuration.
 */
export function renderSpec(asyncapi, params) {
  loadLanguagesConfig();

  const component = React.createElement(AsyncApiComponent, { schema: asyncapi, config: prepareConfiguration(params) });
  return ReactDOMServer.renderToString(component);
}

components/common.js Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
template/index.js Outdated Show resolved Hide resolved
@ritik307
Copy link
Author

@magicmatatjahu Made changes. Kindly review them :)

package.json Outdated Show resolved Hide resolved
Copy link
Member

@magicmatatjahu magicmatatjahu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last thing to fix: https://sonarcloud.io/project/issues?id=asyncapi_html-template&open=AX7pQCNwRTpeUDRjCec4&pullRequest=316&resolved=false&types=CODE_SMELL We need to remove that nested interpolation string. You can make something like:

const styleContentData = styleContent.length ? `<style type="text/css">${styleContent.join('\n')}</style>` : '';

and then reuse it:

${styleContentData ? styleContentData : ''}

@magicmatatjahu
Copy link
Member

@ritik307 Before merging that PR, we need first to resolve that problem asyncapi/generator#521 I will try to focus on it in this week, so that PR atm will be annotated as do-not-merge, ok? Everything is good but first we need to fix mentioned problem. I will ping you when I will find any other problems with that PR, but now we can declare it finished.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@ritik307
Copy link
Author

Before merging that PR, we need first to resolve that problem asyncapi/generator#521 I will try to focus on it in this week, so that PR atm will be annotated as do-not-merge, ok?

Got it 👍

@ritik307
Copy link
Author

@magicmatatjahu Is the linked issue resolved? 😊

@magicmatatjahu
Copy link
Member

@ritik307 If issue is still opened, then no :) It will be closed, when we will merge PR with fix.

@github-actions
Copy link

github-actions bot commented Jul 6, 2022

This pull request has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this pull request, add a comment with detailed explanation.

There can be many reasons why some specific pull request has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this pull request forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions
Copy link

github-actions bot commented Nov 4, 2022

This pull request has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this pull request, add a comment with detailed explanation.

There can be many reasons why some specific pull request has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this pull request forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Nov 4, 2022
@magicmatatjahu
Copy link
Member

Still needs asyncapi/generator#521

@github-actions
Copy link

github-actions bot commented Mar 5, 2023

This pull request has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this pull request, add a comment with detailed explanation.

There can be many reasons why some specific pull request has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this pull request forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Mar 5, 2023
@github-actions github-actions bot closed this Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move to JSX renderer
2 participants