Added CodeEmbed component to embed code snippets in markdowns

This commit is contained in:
amithkoujalgi 2025-04-17 20:31:01 +05:30
parent ec0ebc9654
commit 2b036c8a62
No known key found for this signature in database
GPG Key ID: E29A37746AF94B70
2 changed files with 81 additions and 9 deletions

View File

@ -2,6 +2,8 @@
sidebar_position: 1
---
import CodeEmbed from '@site/src/components/CodeEmbed';
# Models from Ollama Library
These API retrieves a list of models directly from the Ollama library.
@ -11,11 +13,9 @@ These API retrieves a list of models directly from the Ollama library.
This API fetches available models from the Ollama library page, including details such as the model's name, pull count,
popular tags, tag count, and the last update time.
<iframe style={{ width: '100%', height: '919px', border: 'none' }} allow="clipboard-write" src="https://emgithub.com/iframe.html?target=https%3A%2F%2Fgithub.com%2Follama4j%2Follama4j-examples%2Fblob%2Fmain%2Fsrc%2Fmain%2Fjava%2Fio%2Fgithub%2Follama4j%2Fexamples%2FListLibraryModels.java&style=default&type=code&showBorder=on&showLineNumbers=on&showFileMeta=on&showFullPath=on&showCopy=on" />
<a href="https://github.com/ollama4j/ollama4j-examples/blob/main/src/main/java/io/github/ollama4j/examples/ListLibraryModels.java" target="_blank">
View ListLibraryModels.java on GitHub
</a>
<CodeEmbed
src='https://raw.githubusercontent.com/ollama4j/ollama4j-examples/refs/heads/main/src/main/java/io/github/ollama4j/examples/ListLibraryModels.java'>
</CodeEmbed>
The following is the sample output:

View File

@ -0,0 +1,72 @@
import React, { useState, useEffect } from 'react';
import CodeBlock from '@theme/CodeBlock';
const CodeEmbed = ({ src }) => {
const [code, setCode] = useState('');
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
let isMounted = true;
const fetchCodeFromUrl = async (url) => {
if (!isMounted) return;
setLoading(true);
setError(null);
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.text();
if (isMounted) {
setCode(data);
}
} catch (err) {
console.error('Failed to fetch code:', err);
if (isMounted) {
setError(err);
setCode(`// Failed to load code from ${url}\n// ${err.message}`);
}
} finally {
if (isMounted) {
setLoading(false);
}
}
};
if (src) {
fetchCodeFromUrl(src);
}
return () => {
isMounted = false;
};
}, [src]);
const githubUrl = src ? src.replace('https://raw.githubusercontent.com', 'https://github.com').replace('/refs/heads/', '/blob/') : null;
const fileName = src ? src.substring(src.lastIndexOf('/') + 1) : null;
return (
loading ? (
<div>Loading code...</div>
) : error ? (
<div>Error: {error.message}</div>
) : (
<div style={{ backgroundColor: '#f5f5f5', padding: '0px', borderRadius: '5px' }}>
<div style={{ textAlign: 'right' }}>
{githubUrl && (
<a href={githubUrl} target="_blank" rel="noopener noreferrer" style={{ paddingRight: '15px', color: 'gray', fontSize: '0.8em', fontStyle: 'italic', display: 'inline-flex', alignItems: 'center' }}>
View on GitHub
</a>
)}
</div>
<CodeBlock title={fileName} className="language-java">{code}</CodeBlock>
</div>
)
);
};
export default CodeEmbed;