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

fix template readme img src #4426

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion frontend/providers/template/src/pages/api/listTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
import { jsonRes } from '@/services/backend/response';
import { ApiResp } from '@/services/kubernet';
import { TemplateType } from '@/types/app';
import { parseGithubUrl } from '@/utils/tools';
import fs from 'fs';
import type { NextApiRequest, NextApiResponse } from 'next';
import path from 'path';

function replaceRawWithCDN(url: string, cdnUrl: string) {
let parsedUrl = parseGithubUrl(url);
if (!parsedUrl || !cdnUrl) return url;

if (parsedUrl.hostname === 'raw.githubusercontent.com') {
const newUrl = `https://${cdnUrl}/gh/${parsedUrl.organization}/${parsedUrl.repository}@${parsedUrl.branch}/${parsedUrl.remainingPath}`;
return newUrl;
}

return url;
}

export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
const originalPath = process.cwd();
const jsonPath = path.resolve(originalPath, 'fast_deploy_template.json');
const cdnUrl = process.env.CDN_URL;

try {
if (fs.existsSync(jsonPath)) {
const jsonData = fs.readFileSync(jsonPath, 'utf8');
const _templates: TemplateType[] = JSON.parse(jsonData);
const templates = _templates.filter((item) => item?.spec?.draft !== true);

const templates = _templates
.filter((item) => item?.spec?.draft !== true)
.map((item) => {
if (!!cdnUrl) {
item.spec.readme = replaceRawWithCDN(item.spec.readme, cdnUrl);
item.spec.icon = replaceRawWithCDN(item.spec.icon, cdnUrl);
}
return item;
});

return jsonRes(res, { data: templates, code: 200 });
} else {
return jsonRes(res, { data: [], code: 200 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ const ReadMe = ({ templateDetail }: { templateDetail: TemplateType }) => {
// @ts-ignore
const myRewrite = (node, index, parent) => {
if (node.tagName === 'img' && !node.properties.src.startsWith('http')) {
node.properties.src = `https://raw.githubusercontent.com/${githubOptions?.organization}/${githubOptions?.repository}/${githubOptions?.branch}/${node.properties.src}`;
const imgSrc = node.properties.src.replace(/^\.\/|^\//, '');

node.properties.src = `https://${githubOptions?.hostname}/${githubOptions?.organization}/${githubOptions?.repository}/${githubOptions?.branch}/${imgSrc}`;
}
};

Expand All @@ -61,17 +63,15 @@ const ReadMe = ({ templateDetail }: { templateDetail: TemplateType }) => {
borderBottom={'1px solid #DEE0E2'}
color={'#24282C'}
fontSize={'18px'}
fontWeight={500}
>
fontWeight={500}>
<MyIcon name={'markdown'} mr={5} w={'24px'} h={'24px'} ml={'42px'} color={'myGray.500'} />
README.md
</Box>
<Box p={'24px'} className={`markdown-body ${styles.customMarkDownBody}`}>
<ReactMarkdown
linkTarget={'_blank'}
rehypePlugins={[rehypeRaw, [rehypeRewrite, { rewrite: myRewrite }]]}
remarkPlugins={[remarkGfm, remarkUnwrapImages]}
>
remarkPlugins={[remarkGfm, remarkUnwrapImages]}>
{templateReadMe}
</ReactMarkdown>
</Box>
Expand Down
9 changes: 6 additions & 3 deletions frontend/providers/template/src/utils/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,15 @@ export function downLoadBold(content: BlobPart, type: string, fileName: string)

export const parseGithubUrl = (url: string) => {
if (!url) return null;
var urlObj = new URL(url);
var pathParts = urlObj.pathname.split('/');
let urlObj = new URL(url);
let pathParts = urlObj.pathname.split('/');

return {
hostname: urlObj.hostname,
organization: pathParts[1],
repository: pathParts[2],
branch: pathParts[3]
branch: pathParts[3],
remainingPath: pathParts.slice(4).join('/') + urlObj.search
};
};

Expand Down
4 changes: 2 additions & 2 deletions service/license/src/pages/api/cluster/activeCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

const _token = generateLicenseToken({
type: 'Account',
clusterID: clusterId,
clusterID: kubeSystemID,
data: { amount: payment.amount }
});

Expand Down Expand Up @@ -98,7 +98,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const handleStandardAndLicense = async () => {
const _token = generateLicenseToken({
type: 'Account',
clusterID: clusterId,
clusterID: kubeSystemID,
data: { amount: 299 }
});
const record: LicenseRecordPayload = {
Expand Down
3 changes: 1 addition & 2 deletions service/license/src/services/backend/db/payment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { LicenseType, PaymentDB, PaymentStatus, TPayMethod } from '@/types';
import { PaymentDB, PaymentStatus, TPayMethod } from '@/types';
import { connectToDatabase } from './mongodb';
import { createLicenseRecord, generateLicenseToken } from './license';

async function connectPaymentCollection() {
const client = await connectToDatabase();
Expand Down
Loading