mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-26 13:08:28 +00:00
metadata: allow several values and one-liners
This commit is contained in:
parent
98a89aea11
commit
2e22152089
@ -1,14 +1,26 @@
|
|||||||
export function getMetadata(raw_code) {
|
const ALLOW_MANY = ['by', 'url', 'genre', 'license'];
|
||||||
// https://stackoverflow.com/a/15123777
|
|
||||||
const comment_regexp = /\/\*([\s\S]*?)\*\/|([^\\:]|^)\/\/(.*)$/gm;
|
|
||||||
|
|
||||||
const tag_regexp = /@([a-z]*):? (.*)/gm;
|
export function getMetadata(raw_code) {
|
||||||
|
const comment_regexp = /\/\*([\s\S]*?)\*\/|\/\/(.*)$/gm;
|
||||||
const tags = {};
|
const tags = {};
|
||||||
|
|
||||||
for (const match of raw_code.matchAll(comment_regexp)) {
|
for (const match of raw_code.matchAll(comment_regexp)) {
|
||||||
const comment = match[1] ? match[1] : '' + match[3] ? match[3] : '';
|
const tag_matches = (match[1] || match[2] || '').trim().split('@').slice(1);
|
||||||
for (const tag_match of comment.trim().matchAll(tag_regexp)) {
|
for (const tag_match of tag_matches) {
|
||||||
tags[tag_match[1]] = tag_match[2].trim();
|
let [tag, tag_value] = tag_match.split(/ (.*)/s);
|
||||||
|
tag = tag.trim();
|
||||||
|
tag_value = (tag_value || '').replaceAll(/ +/g, ' ').trim();
|
||||||
|
|
||||||
|
if (ALLOW_MANY.includes(tag)) {
|
||||||
|
const tag_list = tag_value
|
||||||
|
.split(/[,\n]/)
|
||||||
|
.map((t) => t.trim())
|
||||||
|
.filter((t) => t !== '');
|
||||||
|
tags[tag] = tag in tags ? tags[tag].concat(tag_list) : tag_list;
|
||||||
|
} else {
|
||||||
|
tag_value = tag_value.replaceAll(/\s+/g, ' ');
|
||||||
|
tags[tag] = tag in tags ? tags[tag] + ' ' + tag_value : tag_value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user