A JavaScript/React library for viewing and rendering nanopublications with beautiful, interactive displays.
β οΈ Note: This library is currently under active development as part of the Science Live Platform project. The API may change. Not yet published to npm.
- π¨ Beautiful Rendering - Clean, readable display of nanopublications
- π Automatic Template Fetching - Automatically retrieves and applies nanopub templates
- βοΈ React Support - Ready-to-use React component with TypeScript support
- π¦ Zero Dependencies - Lightweight library with no runtime dependencies
- π― Vanilla JS - Works in any JavaScript project
- π Scoped Styling - CSS won't conflict with your existing styles
- π Interactive Features - Share, copy, download, and cite nanopublications
- π± Responsive - Works on desktop and mobile devices
Currently not published to npm. Install directly from GitHub:
Requirements: Node.js 20+ (due to Vite 5 requirement)
# Install from GitHub
npm install git+https://github.com/ScienceLiveHub/nanopub-view.git
# Or install a specific branch/tag
npm install git+https://github.com/ScienceLiveHub/nanopub-view.git#main# Clone the repository
git clone https://github.com/ScienceLiveHub/nanopub-view.git
cd nanopub-view
# Install dependencies
npm install
# Build the library
npm run build
# Link for local testing in other projects
npm link
# In your other project (e.g., science-live-platform)
cd ../your-project
npm link @sciencelivehub/nanopub-viewimport { NanopubViewer } from '@sciencelivehub/nanopub-view/react';
import '@sciencelivehub/nanopub-view/dist/nanopub-viewer.css';
function App() {
return (
<NanopubViewer
uri="https://w3id.org/np/RA6Cz33icPZrBAummwxw6MwdS-RepX-sUjW_fZz905Rvc"
onLoad={(data) => console.log('Loaded:', data)}
onError={(err) => console.error('Error:', err)}
/>
);
}<!DOCTYPE html>
<html>
<head>
<!-- Install from GitHub first: npm install git+https://github.com/ScienceLiveHub/nanopub-view.git -->
<link rel="stylesheet" href="node_modules/@sciencelivehub/nanopub-view/dist/nanopub-viewer.css">
<!-- Required for icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
<div id="viewer-container"></div>
<script type="module">
import { NanopubViewer } from './node_modules/@sciencelivehub/nanopub-view/dist/nanopub-viewer.esm.js';
const viewer = new NanopubViewer({
theme: 'default',
showMetadata: true
});
await viewer.renderFromUri(
document.getElementById('viewer-container'),
'https://w3id.org/np/RA6Cz33icPZrBAummwxw6MwdS-RepX-sUjW_fZz905Rvc'
);
</script>
</body>
</html>interface NanopubViewerProps {
/** Nanopublication URI to fetch and display */
uri?: string;
/** Nanopublication content (TriG format) to display directly */
content?: string;
/** Viewer options */
options?: {
apiEndpoint?: string; // Default: 'https://np.petapico.org/'
theme?: string; // Default: 'default'
showMetadata?: boolean; // Default: true
fetchTimeout?: number; // Default: 30000
};
/** Callback when nanopub is successfully loaded */
onLoad?: (data: any) => void;
/** Callback when an error occurs */
onError?: (error: Error) => void;
/** Additional CSS class names */
className?: string;
/** Inline styles */
style?: React.CSSProperties;
}const viewer = new NanopubViewer(options);Options:
apiEndpoint(string): API endpoint for fetching nanopubs. Default:'https://np.petapico.org/'theme(string): Visual theme. Default:'default'showMetadata(boolean): Show metadata section. Default:truefetchTimeout(number): Request timeout in milliseconds. Default:30000
renderFromUri(container, uri)
Fetch and render a nanopublication from a URI.
await viewer.renderFromUri(
document.getElementById('container'),
'https://w3id.org/np/RA...'
);Parameters:
container(HTMLElement | string): Container element or CSS selectoruri(string): Nanopublication URI
Returns: Promise
render(container, content)
Render nanopublication from content string (TriG format).
await viewer.render(
document.getElementById('container'),
trigContent
);Parameters:
container(HTMLElement | string): Container element or CSS selectorcontent(string): Nanopublication content in TriG format
Returns: Promise
The library uses scoped CSS with the .nanopub-viewer class. All styles are contained and won't conflict with your application.
You can override styles by targeting the scoped classes:
/* Override assertion background */
.nanopub-viewer .assertion-box {
background: #f0f9ff !important;
}
/* Override title color */
.nanopub-viewer .pub-title {
color: #1e40af !important;
}The library uses Font Awesome for icons. Include it in your HTML:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"><NanopubViewer uri="https://w3id.org/np/RA..." />const trigContent = `
@prefix : <http://purl.org/nanopub/temp/mynanopub#> .
@prefix np: <http://www.nanopub.org/nschema#> .
...
`;
<NanopubViewer content={trigContent} /><NanopubViewer
uri="https://w3id.org/np/RA..."
onLoad={(data) => {
console.log('Title:', data.title);
console.log('Author:', data.authorName);
}}
onError={(err) => {
alert(`Failed to load: ${err.message}`);
}}
/><NanopubViewer
uri="https://w3id.org/np/RA..."
options={{
apiEndpoint: 'https://custom-api.example.com/',
fetchTimeout: 60000,
showMetadata: false
}}
/>function Gallery() {
const nanopubUris = [
'https://w3id.org/np/RA...',
'https://w3id.org/np/RB...',
'https://w3id.org/np/RC...'
];
return (
<div style={{ display: 'grid', gap: '20px' }}>
{nanopubUris.map(uri => (
<NanopubViewer key={uri} uri={uri} />
))}
</div>
);
}This library is under active development for the Science Live Platform.
Current Status:
- β Core rendering functionality
- β React component wrapper
- β Automatic template fetching
- β Interactive features (share, copy, cite)
- β GitHub Pages demo
- π§ Additional visualization modes (planned)
- π§ Theming system (planned)
- π§ Comprehensive test suite (planned)
- π npm publication (planned after stabilization)
Roadmap:
- Complete integration with Science Live Platform
- Add multiple display modes (compact, card, inline)
- Implement theming system
- Add comprehensive documentation
- Write tests
- Stabilize API
- Publish to npm
Feedback and contributions are welcome! Please open an issue if you encounter problems or have suggestions.
- Node.js 20+ (required by Vite 5)
- npm or yarn
# Clone the repository
git clone https://github.com/ScienceLiveHub/nanopub-view.git
cd nanopub-view
# Install dependencies
npm install
# Start demo server
npm run devThe demo will open at http://localhost:3000
# Build library for distribution
npm run build
# Output: dist/nanopub-viewer.js, dist/nanopub-viewer.esm.js, dist/nanopub-viewer.css# Build demo for GitHub Pages
npm run build:demo
# Output: demo-dist/ (deployed automatically via GitHub Actions)# Preview demo locally
npm run previewBuild System:
- Vite - Fast build tool and dev server
- Multiple outputs:
dist/nanopub-viewer.js- UMD format (for<script>tags)dist/nanopub-viewer.esm.js- ES module (forimportstatements)dist/nanopub-viewer.css- Compiled and scoped styles
- Automatic deployment: Pushing to
maintriggers GitHub Actions to rebuild and deploy the demo
nanopub-view/
βββ .github/
β βββ workflows/
β βββ deploy.yml # GitHub Actions: Auto-deploy demo to GitHub Pages
βββ src/
β βββ core/
β β βββ parser.js # RDF/TriG parsing & template processing
β β βββ renderer.js # HTML generation & display logic
β β βββ fetcher.js # Fetch nanopubs from URIs
β βββ styles/
β β βββ viewer.css # Scoped CSS (prefixed with .nanopub-viewer)
β βββ react/
β β βββ NanopubViewer.tsx # React wrapper component with TypeScript
β βββ index.js # Main entry point (vanilla JS)
β βββ react.js # React-specific exports
βββ demo/
β βββ index.html # Interactive demo page
βββ dist/ # Built files (generated, not committed)
β βββ nanopub-viewer.js # UMD bundle for <script> tags
β βββ nanopub-viewer.esm.js # ES module for import statements
β βββ nanopub-viewer.css # Compiled styles
βββ .gitignore # Git ignore rules
βββ LICENSE # MIT license
βββ README.md # This file
βββ package.json # Package configuration & scripts
βββ vite.config.js # Build configuration for Vite
Key Files Explained:
src/core/- Core library logic (parsing, rendering, fetching)src/react/- React-specific wrapper for easy integrationdemo/- Live demo hosted on GitHub Pagesdist/- Built files for distribution (generated bynpm run build).github/workflows/deploy.yml- Automates demo deployment on push to mainvite.config.js- Configures builds for both library and demopackage.json- Defines library metadata, scripts, and dependencies
Contributions are welcome! This library is part of the Science Live Platform project and is under active development.
How to contribute:
- Report Issues: Found a bug or have a feature request? Open an issue
- Discuss: Have ideas for new features? Start a discussion
- Code: Want to contribute code?
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development Guidelines:
- Keep the library lightweight (zero runtime dependencies)
- Maintain backward compatibility when possible
- Add examples for new features
- Update documentation
- Test in both vanilla JS and React contexts
Since this library is under active development, breaking changes may occur. We'll document them in releases and aim to minimize disruption.
MIT License - see the LICENSE file for details.
- Live Demo: https://sciencelive4all.org/nanopub-view/
- GitHub Repository: https://github.com/ScienceLiveHub/nanopub-view
- Science Live Platform: https://github.com/ScienceLiveHub/science-live-platform
- Nanopublications: http://nanopub.org/
- Built for the Science Live Platform
- Inspired by the nanopublication ecosystem
- Supports RDF/TriG format parsing and rendering
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Science Live Platform: For broader questions about the platform
- Initial implementation
- Core parsing and rendering
- React component wrapper
- Template auto-fetching
- Interactive features
- Stabilized API
- Complete documentation
- Test coverage
- npm publication
Built with β€οΈ for the Science Live Platform
This library enables beautiful, interactive display of nanopublications for the scientific community.