Astro Command unlocks the world

Introduction

This weekend I built a new Astro component that lets callers use external commands on their machine to generate html content.

trashhalo
/
astro-command

run …


This content originally appeared on DEV Community and was authored by Stephen Solka

Introduction

This weekend I built a new Astro component that lets callers use external commands on their machine to generate html content.

GitHub logo trashhalo / astro-command

run commands as astro components

In the repository there is a hello world demo that is shelling out to python.

--------
import { Command } from "astro-command";
--------
<Command caller={import.meta.url} command="./Component.py" message="from python!" />
#!/usr/bin/env python 

import sys, json

data = json.load(sys.stdin)
print(f'<h1> Hello {data["message"]} </h1>')

Props are passed in as a json blob to stdin. Html comes out stdout.

Taking it up a notch

Python has a very deep well of libraries that can be useful for astro page generation. One example is https://matplotlib.org/ a plotting library that can generate beautiful graphs outputting SVG.

As a quick example I rewrote the hello world py component to output a graph instead.

Screenshot 2021-09-19 144501

Its not just python

Now that you have shell access from Astro you can use any command! The first one I created a custom component for is Pandoc to get access to all the types of content Pandoc can understand. With 12 lines of astro code I unlocked 30+ file formats!

Astro Pandoc

Astro component for using pandoc to convert content. This allows you to embed any format pandoc supports.

Usage

---
import { Pandoc } from "astro-pandoc"
---
<Pandoc caller={import.meta.url} file="Component.tex" extraArgs={["--webtex"]} /

Component.tex

\documentclass{article}
\usepackage{amsmath} % for the equation* environment
\begin{document}
This is a simple math expression \(\sqrt{x^2+1}\) inside text. 
And this is also the same: 
\begin{math}
\sqrt{x^2+1}
\end{math}
but by using another command.

This is a simple math expression without numbering
\[\sqrt{x^2+1}\] 
separated from text.

This is also the same:
\begin{displaymath}
\sqrt{x^2+1}
\end{displaymath}

\ldots and this:

Whats next?

There are lots of commands out there. Which one should I connect to astro next?


This content originally appeared on DEV Community and was authored by Stephen Solka


Print Share Comment Cite Upload Translate Updates
APA

Stephen Solka | Sciencx (2021-09-19T19:04:43+00:00) Astro Command unlocks the world. Retrieved from https://www.scien.cx/2021/09/19/astro-command-unlocks-the-world/

MLA
" » Astro Command unlocks the world." Stephen Solka | Sciencx - Sunday September 19, 2021, https://www.scien.cx/2021/09/19/astro-command-unlocks-the-world/
HARVARD
Stephen Solka | Sciencx Sunday September 19, 2021 » Astro Command unlocks the world., viewed ,<https://www.scien.cx/2021/09/19/astro-command-unlocks-the-world/>
VANCOUVER
Stephen Solka | Sciencx - » Astro Command unlocks the world. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/19/astro-command-unlocks-the-world/
CHICAGO
" » Astro Command unlocks the world." Stephen Solka | Sciencx - Accessed . https://www.scien.cx/2021/09/19/astro-command-unlocks-the-world/
IEEE
" » Astro Command unlocks the world." Stephen Solka | Sciencx [Online]. Available: https://www.scien.cx/2021/09/19/astro-command-unlocks-the-world/. [Accessed: ]
rf:citation
» Astro Command unlocks the world | Stephen Solka | Sciencx | https://www.scien.cx/2021/09/19/astro-command-unlocks-the-world/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.