добавить слои в мой файл JSON и заставить его работать с JavaScript - PullRequest
0 голосов
/ 28 июня 2019

Я пытаюсь создать глоссарий для моей веб-страницы, чтобы слова во всей моей веб-странице определялись во всплывающей подсказке, если они включены в мой глоссарий.

Я нашел плагин jquery, который действительно хорошо работаетТем не менее, мне нужно немного изменить его, и у меня нет опыта работы с JSON / Javascript.

это немного похоже на то, как будет выглядеть файл JSON:

  {
    "term" : "termC",
    "description" : "definition C"
  },
  {
    "term": "term D",
    "description": "definition D"
  }
]

Я хочу добавить в файл JSON дополнительный слой, подобный этому:

[
{
      "A" : [
  {
    "term" : "termA",
    "description" : "definition A"
  },
  {
    "term": "term B",
    "description": "definition b"
  }
]
      "B" : [
  {
    "term" : "termC",
    "description" : "definition C"
  },
  {
    "term": "term D",
    "description": "definition D"
  }
]
}

и все еще работаю с javascript.

Я нашел плагин на GitHub в https://github.com/PebbleRoad/glossarizer.javascript - это длинный файл, и, соблюдая правила StackOverflow, я не могу пропустить весь файл, но я считаю, что это важная часть:

    base.terms = []

    /* Excludes array */

    base.excludes = []

    /* Replaced words array */

    base.replaced = []

    /* Regex Tags */

    base.regexOption = (base.options.caseSensitive ? '' : 'i') + (base.options.replaceOnce ? '' : 'g')

    /* Fetch glossary JSON */

    $.getJSON(this.options.sourceURL).then(function (data) {
      base.glossary = data

      if (!base.glossary.length || base.glossary.length == 0) return

      /**
       * Get all terms
       */

      for (var i = 0; i < base.glossary.length; i++) {
        var terms = base.glossary[i].term.split(',')

        for (var j = 0; j < terms.length; j++) {
          /* Trim */

          var trimmed = terms[j].replace(/^\s+|\s+$/g, ''),
            isExclusion = trimmed.indexOf('!')

          if (isExclusion == -1 || isExclusion != 0) {
            /* Glossary terms array */

            base.terms.push(trimmed)
          } else {
            /* Excluded terms array */

            base.excludes.push(trimmed.substr(1))
          }
        }
      }

      /**
       * Wrap terms
       */

      base.wrapTerms()
    })
  }
...