UNPKG

@scienceicons/myst

Version:

Open Science Icons in MyST Markdown by Curvenote

1 lines 15.1 kB
{"version":3,"sources":["../node_modules/nanoid/index.js","../node_modules/myst-common/dist/utils.js","../src/names.json","../src/scienceicons.ts"],"sourcesContent":["import { randomFillSync } from 'crypto'\nimport { urlAlphabet } from './url-alphabet/index.js'\nexport { urlAlphabet }\nconst POOL_SIZE_MULTIPLIER = 128\nlet pool, poolOffset\nlet fillPool = bytes => {\n if (!pool || pool.length < bytes) {\n pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)\n randomFillSync(pool)\n poolOffset = 0\n } else if (poolOffset + bytes > pool.length) {\n randomFillSync(pool)\n poolOffset = 0\n }\n poolOffset += bytes\n}\nexport let random = bytes => {\n fillPool((bytes -= 0))\n return pool.subarray(poolOffset - bytes, poolOffset)\n}\nexport let customRandom = (alphabet, defaultSize, getRandom) => {\n let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1\n let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)\n return (size = defaultSize) => {\n let id = ''\n while (true) {\n let bytes = getRandom(step)\n let i = step\n while (i--) {\n id += alphabet[bytes[i] & mask] || ''\n if (id.length === size) return id\n }\n }\n }\n}\nexport let customAlphabet = (alphabet, size = 21) =>\n customRandom(alphabet, size, random)\nexport let nanoid = (size = 21) => {\n fillPool((size -= 0))\n let id = ''\n for (let i = poolOffset - size; i < poolOffset; i++) {\n id += urlAlphabet[pool[i] & 63]\n }\n return id\n}\n","import { customAlphabet } from 'nanoid';\nfunction addMessageInfo(message, info) {\n if (info === null || info === void 0 ? void 0 : info.note)\n message.note = info.note;\n if (info === null || info === void 0 ? void 0 : info.url)\n message.url = info.url;\n if (info === null || info === void 0 ? void 0 : info.ruleId)\n message.ruleId = info.ruleId;\n if (info === null || info === void 0 ? void 0 : info.fatal)\n message.fatal = true;\n return message;\n}\nexport function fileError(file, message, opts) {\n return addMessageInfo(file.message(message, opts === null || opts === void 0 ? void 0 : opts.node, opts === null || opts === void 0 ? void 0 : opts.source), { ...opts, fatal: true });\n}\nexport function fileWarn(file, message, opts) {\n return addMessageInfo(file.message(message, opts === null || opts === void 0 ? void 0 : opts.node, opts === null || opts === void 0 ? void 0 : opts.source), opts);\n}\nexport function fileInfo(file, message, opts) {\n return addMessageInfo(file.info(message, opts === null || opts === void 0 ? void 0 : opts.node, opts === null || opts === void 0 ? void 0 : opts.source), opts);\n}\nconst az = 'abcdefghijklmnopqrstuvwxyz';\nconst alpha = az + az.toUpperCase();\nconst numbers = '0123456789';\nconst nanoidAZ = customAlphabet(alpha, 1);\nconst nanoidAZ9 = customAlphabet(alpha + numbers, 9);\n/**\n * Create random 10-digit alphanumeric string\n */\nexport function createId() {\n return nanoidAZ() + nanoidAZ9();\n}\n/**\n * https://github.com/syntax-tree/mdast#association\n * @param label A label field can be present.\n * label is a string value: it works just like title on a link or a\n * lang on code: character escapes and character references are parsed.\n * @returns { identifier, label, html_id }\n */\nexport function normalizeLabel(label) {\n if (!label)\n return undefined;\n const identifier = label\n .replace(/[\\t\\n\\r ]+/g, ' ')\n .replace(/['‘’\"“”]+/g, '') // These can make matching difficult, especially in glossaries and terms\n .trim()\n .toLowerCase();\n const html_id = createHtmlId(identifier);\n return { identifier, label: label, html_id };\n}\nexport function createHtmlId(identifier) {\n if (!identifier)\n return undefined;\n return identifier\n .toLowerCase()\n .replace(/[^a-z0-9-]/g, '-') // Remove any fancy characters\n .replace(/^([0-9-])/, 'id-$1') // Ensure that the id starts with a letter\n .replace(/-[-]+/g, '-') // Replace repeated `-`s\n .replace(/(?:^[-]+)|(?:[-]+$)/g, ''); // Remove repeated `-`s at the start or the end\n}\n/**\n * Transfer all target-related attributes from one node to another\n *\n * During mdast transformation, these attributes (including: label,\n * identifier, html_id, indexEntries) are often moved. For example\n * `mystTarget` information propagates to the next node, `image`\n * attributes may propagate to parent `container` node, etc.\n *\n * This shared function helps insure attributes are not lost along the way.\n */\nexport function transferTargetAttrs(sourceNode, destNode, vfile) {\n if (sourceNode.label) {\n if (destNode.label && vfile) {\n fileWarn(vfile, `label \"${destNode.label}\" replaced with \"${sourceNode.label}\"`, {\n node: destNode,\n });\n }\n destNode.label = sourceNode.label;\n delete sourceNode.label;\n }\n if (sourceNode.identifier) {\n destNode.identifier = sourceNode.identifier;\n delete sourceNode.identifier;\n }\n if (sourceNode.html_id) {\n destNode.html_id = sourceNode.html_id;\n delete sourceNode.html_id;\n }\n if (sourceNode.indexEntries) {\n if (!destNode.indexEntries)\n destNode.indexEntries = [];\n destNode.indexEntries.push(...sourceNode.indexEntries);\n delete sourceNode.indexEntries;\n }\n}\n/**\n * Helper function for recursively lifting children\n */\nfunction getNodeOrLiftedChildren(node, removeType) {\n if (!node.children)\n return [node];\n const children = node.children.map((child) => getNodeOrLiftedChildren(child, removeType)).flat();\n if (node.type === removeType) {\n // There are some checks in unist that look like `'children' in node`\n // all children must be deleted, and not a key on the object\n if (node && node.children == null)\n delete node.children;\n return children;\n }\n node.children = children;\n return [node];\n}\n/**\n * Eliminate all parent nodes in `tree` of type `removeType`; children of eliminated nodes are moved up to it's parent\n *\n * Nodes of `removeType` will remain if:\n * - they are the root of `tree`\n * - their children are undefined\n */\nexport function liftChildren(tree, removeType) {\n if (!tree.children)\n return;\n tree.children = tree.children.map((child) => getNodeOrLiftedChildren(child, removeType)).flat();\n}\nexport function setTextAsChild(node, text) {\n node.children = [{ type: 'text', value: text }];\n}\n/**\n * Renders a textual representation of one or more nodes\n * by concatenating all children that have a text representation.\n * @param content The node or nodes to provide as input.\n * @returns A string. An empty string is returned in case no\n * textual representation could be extracted.\n */\nexport function toText(content) {\n if (!content)\n return '';\n if (!Array.isArray(content))\n return toText([content]);\n return content\n .map((n) => {\n if (!n || typeof n === 'string')\n return n || '';\n if ('value' in n)\n return n.value;\n if ('children' in n && n.children)\n return toText(n.children);\n return '';\n })\n .join('');\n}\nexport function copyNode(node) {\n return structuredClone(node);\n}\nexport function mergeTextNodes(node) {\n var _a;\n const children = (_a = node.children) === null || _a === void 0 ? void 0 : _a.reduce((c, n) => {\n var _a;\n if ((n === null || n === void 0 ? void 0 : n.type) !== 'text') {\n c.push(mergeTextNodes(n));\n return c;\n }\n const last = c[c.length - 1];\n if ((last === null || last === void 0 ? void 0 : last.type) !== 'text') {\n c.push(n);\n return c;\n }\n if ((_a = n.position) === null || _a === void 0 ? void 0 : _a.end) {\n if (!last.position)\n last.position = {};\n last.position.end = n.position.end;\n }\n if (!last.value)\n last.value = '';\n if (n.value)\n last.value += n.value;\n return c;\n }, []);\n if (children)\n node.children = children;\n return node;\n}\nexport function admonitionKindToTitle(kind) {\n const transform = {\n attention: 'Attention',\n caution: 'Caution',\n danger: 'Danger',\n error: 'Error',\n important: 'Important',\n hint: 'Hint',\n note: 'Note',\n seealso: 'See Also',\n tip: 'Tip',\n warning: 'Warning',\n };\n return transform[kind] || `Unknown Admonition \"${kind}\"`;\n}\nexport function writeTexLabelledComment(title, commands, commentLength) {\n if (!commands || (commands === null || commands === void 0 ? void 0 : commands.length) === 0)\n return '';\n const len = (commentLength - title.length - 4) / 2;\n const start = ''.padEnd(Math.ceil(len), '%');\n const end = ''.padEnd(Math.floor(len), '%');\n const titleBlock = `${start} ${title} ${end}\\n`;\n return `${titleBlock}${commands.join('\\n')}\\n`;\n}\nexport function getMetadataTags(node) {\n var _a;\n if (!node.data)\n return [];\n const tags = (_a = node.data.tags) !== null && _a !== void 0 ? _a : [];\n Object.entries(node.data).forEach(([key, val]) => {\n if (val === true || (typeof val === 'string' && val.toLowerCase() === 'true')) {\n tags.push(key);\n }\n });\n return tags.map((tag) => tag.toLowerCase());\n}\n","[\n { \"name\": \"arxiv\", \"componentName\": \"ArxivIcon\" },\n { \"name\": \"cc-by\", \"componentName\": \"CcByIcon\" },\n { \"name\": \"cc-nc\", \"componentName\": \"CcNcIcon\" },\n { \"name\": \"cc-nd\", \"componentName\": \"CcNdIcon\" },\n { \"name\": \"cc-sa\", \"componentName\": \"CcSaIcon\" },\n { \"name\": \"cc-zero\", \"componentName\": \"CcZeroIcon\" },\n { \"name\": \"cc\", \"componentName\": \"CcIcon\" },\n { \"name\": \"curvenote\", \"componentName\": \"CurvenoteIcon\" },\n { \"name\": \"discord\", \"componentName\": \"DiscordIcon\" },\n { \"name\": \"discourse\", \"componentName\": \"DiscourseIcon\" },\n { \"name\": \"email\", \"componentName\": \"EmailIcon\" },\n { \"name\": \"github\", \"componentName\": \"GithubIcon\" },\n { \"name\": \"jupyter-book\", \"componentName\": \"JupyterBookIcon\" },\n { \"name\": \"jupyter-text\", \"componentName\": \"JupyterTextIcon\" },\n { \"name\": \"jupyter\", \"componentName\": \"JupyterIcon\" },\n { \"name\": \"linkedin\", \"componentName\": \"LinkedinIcon\" },\n { \"name\": \"mastodon\", \"componentName\": \"MastodonIcon\" },\n { \"name\": \"myst\", \"componentName\": \"MystIcon\" },\n { \"name\": \"open-access\", \"componentName\": \"OpenAccessIcon\" },\n { \"name\": \"orcid\", \"componentName\": \"OrcidIcon\" },\n { \"name\": \"osi\", \"componentName\": \"OsiIcon\" },\n { \"name\": \"ror\", \"componentName\": \"RorIcon\" },\n { \"name\": \"slack\", \"componentName\": \"SlackIcon\" },\n { \"name\": \"twitter\", \"componentName\": \"TwitterIcon\" },\n { \"name\": \"website\", \"componentName\": \"WebsiteIcon\" },\n { \"name\": \"youtube\", \"componentName\": \"YoutubeIcon\" }\n]\n","import type { CurvenotePlugin } from '@curvenote/common';\nimport { fileWarn } from 'myst-common';\nimport type { GenericNode, RoleSpec } from 'myst-common';\nimport names from './names.json';\n\nconst SUPPORTED_ICONS = names.map((icon) => icon.name);\n\nconst iconRole: RoleSpec = {\n name: 'scienceicon',\n alias: ['scicon'],\n body: {\n type: String,\n required: true,\n doc: 'The kind of icon to display',\n },\n run(data, vfile) {\n if (!SUPPORTED_ICONS.includes(data.body as string)) {\n fileWarn(vfile, `Unknown name of scienceicon: ${data.body}`);\n return [];\n }\n\n return [{ type: 'scienceicon', value: data.body, kind: data.body }] as GenericNode[];\n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