4
1

getPassageLinks.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const passageLinkMarkups:{[key:string]:{type:string, regex: RegExp}} = {
  2. 'gs-macro': {type: 'gs', regex: /<<gs\s+`\['([^']+)'[^\]]*\]`\s*>>/gd},
  3. 'gt-function': {type: 'gt', regex: /setup\.qsp_gt\('([^']+)'[^\)]*\s*\)/gd},
  4. 'goto': {type: 'gt', regex: /<<goto\s+'([^']+)'\s*>>/gd},
  5. 'include': {type: 'gs', regex: /<<include\s+'([^']+)'\s*>>/gd},
  6. };
  7. setup.getPassageLinks = function(passageId:string){
  8. let result:{[passageId: string]: {type: string;line: number;}[];} = {};
  9. const passageCode = Story.get(passageId).text;
  10. for(const[key, markupSettings] of Object.entries(passageLinkMarkups)){
  11. const regex = markupSettings.regex;
  12. let regexMatch;
  13. while(regexMatch = regex.exec(passageCode)) {
  14. const passageId = regexMatch[1];
  15. result[passageId] ??= [];
  16. result[passageId].push({line:-1, type: markupSettings.type});
  17. }
  18. }
  19. return result;
  20. }
  21. setup.getPassageLinksToPassage = function(passageId:string){
  22. function anyPassageLinkMarkupMatches(text:string):boolean{
  23. for(const[key, markupSettings] of Object.entries(passageLinkMarkups)){
  24. let regexMatch;
  25. while(regexMatch = markupSettings.regex.exec(text))
  26. if(regexMatch[1] == passageId)
  27. return true;
  28. }
  29. return false;
  30. }
  31. return Story.lookupWith(function(p){return anyPassageLinkMarkupMatches(p.text)}).map((passage)=>passage.title);
  32. }