OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-editor-parser / src / document / to-display.js

128 lines · javascript ref:
const {
    DISPLAY_TYPES: t,
    NODE_TYPE: e,
    MARK_TYPE: n,
    LINK_PROTOCOL: s,
    MENTION_PREFIX: r,
    BREAKLINE_CHAR: a,
  } = require("../constants.js"),
  { normalizeUrl: c } = require("../utils.js"),
  {
    MENTION: o,
    HTTP_LINK: u,
    PEAR_LINK: i,
    BOLD: l,
    ITALIC: E,
    CODE: p,
    EMOJI: O,
    CODE_BLOCK: h,
    STRIKE_THROUGH: T,
  } = t
function I(t) {
  return y(t)
}
function y(t, e = 0) {
  let n = "",
    s = []
  if (L(t)) ((n = f(t)), (s = R(t, e)))
  else {
    const r = e
    let c = "",
      o = []
    for (const [n, s] of t.content.entries()) {
      const r = y(s, e)
      ;(b(t, n) && (r.text += a), (c += r.text), (o = o.concat(r.display)), (e += r.text.length))
    }
    n = f(t, c) || c
    const u = R(t, r, n.length)
    s = Object.keys(u).length > 0 ? u : o
  }
  return { text: n, display: s }
}
function f(t, n = "") {
  let s = ""
  switch (t.type) {
    case e.TEXT:
      s = t.text
      break
    case e.EMOJI:
      s = t.attrs["data-emoji"]
      break
    case e.MENTION:
      s = `${r}${t.attrs.label}`
      break
    case e.CODE_BLOCK:
      s = ` ${n} `
      break
    case e.HARD_BREAK:
      s = a
  }
  return s
}
function R(t, n, s = 0) {
  const a = []
  switch (t.type) {
    case e.TEXT: {
      const e = t.marks || []
      for (const s of e) {
        const e = A(s)
        if (e) {
          const { type: s, ...r } = e
          a.push({ type: s, start: n, length: t.text.length, ...r })
        }
      }
      break
    }
    case e.EMOJI:
      a.push({
        type: O,
        start: n,
        length: t.attrs["data-emoji"].length,
        content: t.attrs["data-shortcode"],
      })
      break
    case e.MENTION:
      a.push({ type: o, start: n, length: r.length + t.attrs.label.length, memberId: t.attrs.id })
      break
    case e.CODE_BLOCK:
      a.push({ type: h, start: n + 1, length: Math.max(0, s - 2) })
  }
  return a
}
function A(t) {
  let e
  switch (t.type) {
    case n.BOLD:
      e = { type: l }
      break
    case n.ITALIC:
      e = { type: E }
      break
    case n.STRIKE_THROUGH:
      e = { type: T }
      break
    case n.CODE:
      e = { type: p }
      break
    case n.LINK:
      P(t) ? (e = { type: u, content: N(t) }) : _(t) && (e = { type: i, content: N(t) })
  }
  return e
}
function b(t, n) {
  return t.content[n].type === e.PARAGRAPH && t.content[n + 1]?.type === e.PARAGRAPH
}
function L(t) {
  return !t.content
}
function N(t) {
  return c(t.attrs.href)
}
function P(t) {
  return N(t).startsWith(s.HTTP)
}
function _(t) {
  return N(t).startsWith(s.PEAR)
}
module.exports = { documentToDisplay: I }