Reading an index from a dictionary in js

I am writing a widget where I need to fetch a value from a tw dictionary. But I don’t know how to do this in javascript. So I looked at transclude.js source fo the transcludewidget.

The things that seems to do what I’m looking for seems to be that for “text/raw” in the execute method:

	switch(this.transcludeOutput || "text/html") {
		case "text/html":
			// Return the parse tree nodes of the target
			target = this.parseTransclusionTarget(parseAsInline);
			this.parseAsInline = target.parseAsInline;
			parseTreeNodes = target.parseTreeNodes;
			break;
		case "text/raw":
			// Just return the raw text
			target = this.getTransclusionTarget();
			parseTreeNodes = [{type: "text", text: target.text}];
			break;

and further on:

		// Transcluding a text reference
		var parserInfo = this.wiki.getTextReferenceParserInfo(
						this.transcludeTitle,
						this.transcludeField,
						this.transcludeIndex,
						{
							subTiddler: this.transcludeSubTiddler,
							defaultType: this.transcludeType
						});
		return {
			text: parserInfo.text,
			type: parserInfo.type
		};

with, in my case, this.transcludeSubTiddler and his.transcludeType being null (?). and this.transcludeField also (or an empty string).

An other possibility is this code in wiki.js:

exports.parseTextReference = function(title,field,index,options) {

	var tiddler,
		text,
		parserInfo;
	if(!options.subTiddler) {
		tiddler = this.getTiddler(title);
		if(field === "text" || (!field && !index)) {
			this.getTiddlerText(title); // Force the tiddler to be lazily loaded
			return this.parseTiddler(title,options);
		}
	} 
	parserInfo = this.getTextReferenceParserInfo(title,field,index,options);
	if(parserInfo.sourceText !== null) {
		return this.parseText(parserInfo.parserType,parserInfo.sourceText,options);
	} else {
		return null;
	}
};

My tries so far are sot successful. Could you tell me if I am totally wrong?

As for my project, I currently use the include widget in my own widget. if both the dictionary and the index exists, this works OK. If not, I can’t manage that. Perhaps a $default attribute would be nice for that. But as of now, my only hope lies on the present quest!

Use wiki.extractTiddlerDataItem:

1 Like

thank you @saqimtiaz . I’m trying that.

But I was not idle. I’ve gone with the other idea and was able to make my way with the following code:

      let indexContent = "";
      const options = {
        subTiddler: null,
        defaultType: "text/plain"
      };
      const parserInfo = this.wiki.getTextReferenceParserInfo("myDict", "", "myIndex", options);
      if (parserInfo.sourceText !== null) {
        const parsed = this.wiki.parseText(parserInfo.parserType, parserInfo.sourceText ,options);
        indexContent = parsed.source.trimEnd();
      } 

I even could simplify it to:

let indexContent = "";
const parserInfo = this.wiki.getTextReferenceParserInfo("myDict", "", "myIndex", {});
if (parserInfo.sourceText !== null) {
    indexContent = parserInfo.sourceText;
}

The code hinted by @saqimtiaz is ven simpler:

const indexContent = this.wiki.extractTiddlerDataItem("myDict", "myIndex", "");