load titles from metadata in examples page

This commit is contained in:
Roipoussiere 2023-06-05 20:42:41 +02:00
parent f9ad5f0d01
commit 52641db590
3 changed files with 20 additions and 17 deletions

View File

@ -1,6 +1,8 @@
---
import * as tunes from '../../../src/repl/tunes.mjs';
import HeadCommon from '../../components/HeadCommon.astro';
import { getMetadata } from '../metadata_parser';
---
<head>
@ -12,7 +14,7 @@ import HeadCommon from '../../components/HeadCommon.astro';
Object.entries(tunes).map(([name, tune]) => (
<a class="rounded-md bg-slate-900 hover:bg-slate-700 cursor-pointer relative" href={`./#${btoa(tune)}`}>
<div class="absolute w-full h-full flex justify-center items-center">
<span class="bg-slate-800 p-2 rounded-md text-white">{name}</span>
<span class="bg-slate-800 p-2 rounded-md text-white">{getMetadata(tune)['title'] || name}</span>
</div>
<img src={`./img/example-${name}.png`} />
</a>

View File

@ -0,0 +1,16 @@
export function getMetadata(raw_code) {
// https://stackoverflow.com/a/15123777
const comment_regexp = /\/\*([\s\S]*?)\*\/|([^\\:]|^)\/\/(.*)$/gm;
const tag_regexp = /@([a-z]*):? (.*)/gm
const tags = {};
for (const match of raw_code.matchAll(comment_regexp)) {
const comment = match[1] ? match[1] : '' + match[3] ? match[3] : '';
for (const tag_match of comment.trim().matchAll(tag_regexp)) {
tags[tag_match[1]] = tag_match[2].trim()
}
}
return tags;
}

View File

@ -1,19 +1,4 @@
export function getMetadata(raw_code) {
// https://stackoverflow.com/a/15123777
const comment_regexp = /\/\*([\s\S]*?)\*\/|([^\\:]|^)\/\/(.*)$/gm;
const tag_regexp = /@([a-z]*):? (.*)/gm
const tags = {};
for (const match of raw_code.matchAll(comment_regexp)) {
const comment = match[1] ? match[1] : '' + match[3] ? match[3] : '';
for (const tag_match of comment.trim().matchAll(tag_regexp)) {
tags[tag_match[1]] = tag_match[2].trim()
}
}
return tags;
}
import { getMetadata } from '../metadata_parser';
export function getMyPatterns() {
const my = import.meta.glob('../../../../my-patterns/**', { as: 'raw', eager: true });