ext_hypernets.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const HYP_REGEX = /<(?!e:|l:)[^,> ]*>?/g;
  2. const HYP_TRIGGER = () => CFG.useHypernetworks && tagword.match(HYP_REGEX);
  3. class HypernetParser extends BaseTagParser {
  4. parse() {
  5. // Show hypernetworks
  6. let tempResults = [];
  7. if (tagword !== "<" && tagword !== "<h:" && tagword !== "<hypernet:") {
  8. let searchTerm = tagword.replace("<hypernet:", "").replace("<h:", "").replace("<", "");
  9. let filterCondition = x => x.toLowerCase().includes(searchTerm) || x.toLowerCase().replaceAll(" ", "_").includes(searchTerm);
  10. tempResults = hypernetworks.filter(x => filterCondition(x)); // Filter by tagword
  11. } else {
  12. tempResults = hypernetworks;
  13. }
  14. // Add final results
  15. let finalResults = [];
  16. tempResults.forEach(t => {
  17. let result = new AutocompleteResult(t.trim(), ResultType.hypernetwork)
  18. result.meta = "Hypernetwork";
  19. finalResults.push(result);
  20. });
  21. return finalResults;
  22. }
  23. }
  24. async function load() {
  25. if (hypernetworks.length === 0) {
  26. try {
  27. hypernetworks = (await readFile(`${tagBasePath}/temp/hyp.txt`)).split("\n")
  28. .filter(x => x.trim().length > 0) //Remove empty lines
  29. .map(x => x.trim()); // Remove carriage returns and padding if it exists
  30. } catch (e) {
  31. console.error("Error loading hypernetworks.txt: " + e);
  32. }
  33. }
  34. }
  35. function sanitize(tagType, text) {
  36. if (tagType === ResultType.hypernetwork) {
  37. return `<hypernet:${text}:${CFG.extraNetworksDefaultMultiplier}>`;
  38. }
  39. return null;
  40. }
  41. PARSERS.push(new HypernetParser(HYP_TRIGGER));
  42. // Add our utility functions to their respective queues
  43. QUEUE_FILE_LOAD.push(load);
  44. QUEUE_SANITIZE.push(sanitize);