var textwords = {}, keywords = {
  allTags: true,
  closeAll: true,
  list: true,
  newJournal: true,
  newTiddler: true,
  permaview: true,
  saveChanges: true,
  search: true,
  slider: true,
  tabs: true,
  tag: true,
  tagging: true,
  tags: true,
  tiddler: true,
  timeline: true,
  today: true,
  version: true,
  option: true,
  with: true,
  filter: true
}, isSpaceName = /[\w_\-]/i, reHR = /^\-\-\-\-+$/, reWikiCommentStart = /^\/\*\*\*$/, reWikiCommentStop = /^\*\*\*\/$/, reBlockQuote = /^<<<$/, reJsCodeStart = /^\/\/\{\{\{$/, reJsCodeStop = /^\/\/\}\}\}$/, reXmlCodeStart = /^<!--\{\{\{-->$/, reXmlCodeStop = /^<!--\}\}\}-->$/, reCodeBlockStart = /^\{\{\{$/, reCodeBlockStop = /^\}\}\}$/, reUntilCodeStop = /.*?\}\}\}/;
function chain(e, v, y) {
  return v.tokenize = y, y(e, v);
}
function tokenBase(v, x) {
  var S = v.sol(), C = v.peek();
  if (x.block = false, S && /[<\/\*{}\-]/.test(C)) {
    if (v.match(reCodeBlockStart)) return x.block = true, chain(v, x, twTokenCode);
    if (v.match(reBlockQuote)) return "quote";
    if (v.match(reWikiCommentStart) || v.match(reWikiCommentStop) || v.match(reJsCodeStart) || v.match(reJsCodeStop) || v.match(reXmlCodeStart) || v.match(reXmlCodeStop)) return "comment";
    if (v.match(reHR)) return "contentSeparator";
  }
  if (v.next(), S && /[\/\*!#;:>|]/.test(C)) {
    if (C == "!") return v.skipToEnd(), "header";
    if (C == "*") return v.eatWhile("*"), "comment";
    if (C == "#") return v.eatWhile("#"), "comment";
    if (C == ";") return v.eatWhile(";"), "comment";
    if (C == ":") return v.eatWhile(":"), "comment";
    if (C == ">") return v.eatWhile(">"), "quote";
    if (C == "|") return "header";
  }
  if (C == "{" && v.match("{{")) return chain(v, x, twTokenCode);
  if (/[hf]/i.test(C) && /[ti]/i.test(v.peek()) && v.match(/\b(ttps?|tp|ile):\/\/[\-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i)) return "link";
  if (C == '"') return "string";
  if (C == "~" || /[\[\]]/.test(C) && v.match(C)) return "brace";
  if (C == "@") return v.eatWhile(isSpaceName), "link";
  if (/\d/.test(C)) return v.eatWhile(/\d/), "number";
  if (C == "/") {
    if (v.eat("%")) return chain(v, x, twTokenComment);
    if (v.eat("/")) return chain(v, x, twTokenEm);
  }
  if (C == "_" && v.eat("_")) return chain(v, x, twTokenUnderline);
  if (C == "-" && v.eat("-")) {
    if (v.peek() != " ") return chain(v, x, twTokenStrike);
    if (v.peek() == " ") return "brace";
  }
  return C == "'" && v.eat("'") ? chain(v, x, twTokenStrong) : C == "<" && v.eat("<") ? chain(v, x, twTokenMacro) : (v.eatWhile(/[\w\$_]/), textwords.propertyIsEnumerable(v.current()) ? "keyword" : null);
}
function twTokenComment(e, v) {
  for (var y = false, b; b = e.next(); ) {
    if (b == "/" && y) {
      v.tokenize = tokenBase;
      break;
    }
    y = b == "%";
  }
  return "comment";
}
function twTokenStrong(e, v) {
  for (var y = false, b; b = e.next(); ) {
    if (b == "'" && y) {
      v.tokenize = tokenBase;
      break;
    }
    y = b == "'";
  }
  return "strong";
}
function twTokenCode(e, v) {
  var y = v.block;
  return y && e.current() ? "comment" : !y && e.match(reUntilCodeStop) || y && e.sol() && e.match(reCodeBlockStop) ? (v.tokenize = tokenBase, "comment") : (e.next(), "comment");
}
function twTokenEm(e, v) {
  for (var y = false, b; b = e.next(); ) {
    if (b == "/" && y) {
      v.tokenize = tokenBase;
      break;
    }
    y = b == "/";
  }
  return "emphasis";
}
function twTokenUnderline(e, v) {
  for (var y = false, b; b = e.next(); ) {
    if (b == "_" && y) {
      v.tokenize = tokenBase;
      break;
    }
    y = b == "_";
  }
  return "link";
}
function twTokenStrike(e, v) {
  for (var y = false, b; b = e.next(); ) {
    if (b == "-" && y) {
      v.tokenize = tokenBase;
      break;
    }
    y = b == "-";
  }
  return "deleted";
}
function twTokenMacro(e, y) {
  if (e.current() == "<<") return "meta";
  var b = e.next();
  return b ? b == ">" && e.peek() == ">" ? (e.next(), y.tokenize = tokenBase, "meta") : (e.eatWhile(/[\w\$_]/), keywords.propertyIsEnumerable(e.current()) ? "keyword" : null) : (y.tokenize = tokenBase, null);
}
const tiddlyWiki = {
  name: "tiddlywiki",
  startState: function() {
    return { tokenize: tokenBase };
  },
  token: function(e, v) {
    return e.eatSpace() ? null : v.tokenize(e, v);
  }
};
export {
  tiddlyWiki
};
