This commit is contained in:
Cleon 2023-01-05 14:19:17 +13:00
parent 7fcd5bbf2c
commit db7ac7fbf1
4 changed files with 40 additions and 47 deletions

View file

@ -1,8 +1,27 @@
export class CodeBlockInfo{
public language:string;
public params:string[];
constructor( language: string, params?:string[] ){
this.language = language;
this.params = params ?? [];
}
}
export class CodeBlockInfoHelper {
public static isRunable( info: CodeBlockInfo ):boolean{
return ['js', 'javascript'].contains( info.language )
&& info.params.at(0) === 'meld-build'
&& info.params.at(1) !== 'skip'
;
}
public static isConsumable( info: CodeBlockInfo ) : boolean{
return info.params.at(0) !== 'meld-build';
}
}

View file

@ -9,9 +9,9 @@ import { Parser } from "src/parser";
import { RunLogger } from 'src/run-logger';
import { TRunContext } from "src/run-context";
import { NamedCodeBlock } from "./named-code-block";
import { CodeBlockInfoHelper } from "./code-block-info";
export class Compiler{
private templateLanguages = ['html', 'css'];
public compile( logger: RunLogger, editor: Editor, view: MarkdownView ) : () => void {
@ -52,23 +52,18 @@ export class Compiler{
// runable code blocks
const runableCodeBlocks = allCodeBlocks
.filter( cb =>
['js', 'javascript'].contains( cb.info.language)
&& cb.info.params.contains('meld-build')
)
.filter( cb => CodeBlockInfoHelper.isRunable( cb.info ) )
;
if (runableCodeBlocks.length == 0){
if ( runableCodeBlocks.length == 0 ){
return null;
}
// source code
const sourceCode = runableCodeBlocks.map( e=>e.content ).join('\n');
// templates
const templateBlocks = allCodeBlocks
.filter( cb =>
this.templateLanguages.contains( cb.info.language)
)
// consumable blocks
const consumableBlocks = allCodeBlocks
.filter( cb => CodeBlockInfoHelper.isConsumable( cb.info ) )
;
@ -76,7 +71,7 @@ export class Compiler{
sourceCode: sourceCode,
data: data,
templates: templateBlocks,
blocks: consumableBlocks,
logger : log,
log: async (...x) => await log.info( ...x ),
@ -161,7 +156,7 @@ export class Compiler{
io: {
import : async (filepath) => await this.import( log, data, templateBlocks, filepath ),
import : async (filepath) => await this.import( log, data, consumableBlocks, filepath ),
async load(path):Promise<string|undefined> {
const filepath = Utils.getSameFolderFilepath(path);
@ -176,9 +171,7 @@ export class Compiler{
},
load_data: async (filepath, name) => await this.data_loader( data, filepath, name ),
//load_template: (filepath) => this.template_loader(templateBlocks, filepath),
async load_data_url(path, mimetype) : Promise<string|undefined> {
const filepath = Utils.getSameFolderFilepath(path);
@ -267,27 +260,10 @@ export class Compiler{
return resultDataSet;
}
private async template_loader(templates: string[], filepath:string) : Promise<string>{
let resultContent = '';
const absFilepath = this.getAbsoluteFilepathFromActiveFile(filepath);
if (!absFilepath){
return resultContent;
}
const file = app.vault.getAbstractFileByPath(absFilepath);
if (file instanceof TFile){
resultContent = await app.vault.read( file );
templates.push(resultContent);
}
return resultContent;
}
private async import(
log: RunLogger,
data: IDataSetCollection,
templates: NamedCodeBlock[],
consumableBlocks: NamedCodeBlock[],
path:string
) : Promise<boolean>{
@ -314,8 +290,7 @@ export class Compiler{
file.basename,
content,
data,
templates,
this.templateLanguages
consumableBlocks
);
return true;
}else{

View file

@ -1,7 +1,7 @@
import { DataSet, DataSetRow, IDataSetCollection } from "src/data-set";
import { CachedMetadata, Editor } from "obsidian";
import { NamedCodeBlock } from "./named-code-block";
import { CodeBlockInfo } from "./code-block-info";
import { CodeBlockInfo, CodeBlockInfoHelper } from "./code-block-info";
export class Parser {
@ -129,8 +129,7 @@ export class Parser {
name:string,
content:string,
data:IDataSetCollection,
templates:NamedCodeBlock[],
templateLanguageFilter: string[]
consumableCodeBlocks:NamedCodeBlock[]
) : void {
const lines = content.split('\n');
@ -159,24 +158,24 @@ export class Parser {
const dataPropName = this.extractAsTableName( currentHeader.length > 0 ? currentHeader : name );
data[dataPropName] = this.parseMdTable(tableLines);
} else if ( codeBlockInfo && templateLanguageFilter.contains( codeBlockInfo.language ) ){
} else if ( codeBlockInfo && CodeBlockInfoHelper.isConsumable( codeBlockInfo ) ){
// extract template codeblock
const templateLines: string[] = [];
// extract consumable codeblock
const blockLines: string[] = [];
i++;
while( i < lines.length && !lines[i].trim().startsWith('```') ){
const templateLine = lines[i];
templateLines.push(templateLine);
const blockLine = lines[i];
blockLines.push(blockLine);
i++;
}
const codeBlock = new NamedCodeBlock(
codeBlockInfo,
name,
templateLines.join('\n')
blockLines.join('\n')
);
templates.push( codeBlock );
consumableCodeBlocks.push( codeBlock );
}
}

View file

@ -8,7 +8,7 @@ export type TRunContext = {
sourceCode: string;
data: IDataSetCollection;
templates: NamedCodeBlock[];
blocks: NamedCodeBlock[];
logger: RunLogger,
log( ...params: any[] ) : Promise<void>;