association.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * @typedef {import('../types.js').AssociationId} AssociationId
  3. */
  4. import {decodeString} from 'micromark-util-decode-string'
  5. /**
  6. * Get an identifier from an association to match it to others.
  7. *
  8. * Associations are nodes that match to something else through an ID:
  9. * <https://github.com/syntax-tree/mdast#association>.
  10. *
  11. * The `label` of an association is the string value: character escapes and
  12. * references work, and casing is intact.
  13. * The `identifier` is used to match one association to another:
  14. * controversially, character escapes and references don’t work in this
  15. * matching: `&copy;` does not match `©`, and `\+` does not match `+`.
  16. *
  17. * But casing is ignored (and whitespace) is trimmed and collapsed: ` A\nb`
  18. * matches `a b`.
  19. * So, we do prefer the label when figuring out how we’re going to serialize:
  20. * it has whitespace, casing, and we can ignore most useless character
  21. * escapes and all character references.
  22. *
  23. * @type {AssociationId}
  24. */
  25. export function association(node) {
  26. if (node.label || !node.identifier) {
  27. return node.label || ''
  28. }
  29. return decodeString(node.identifier)
  30. }