|
| 1 | +const h = require('hastscript') |
| 2 | +const octicons = require('@primer/octicons') |
| 3 | +const parse5 = require('parse5') |
| 4 | +const fromParse5 = require('hast-util-from-parse5') |
| 5 | + |
| 6 | +const LANGUAGE_MAP = { |
| 7 | + asp: 'ASP', |
| 8 | + aspx: 'ASP', |
| 9 | + 'aspx-vb': 'ASP', |
| 10 | + as3: 'ActionScript', |
| 11 | + apache: 'ApacheConf', |
| 12 | + nasm: 'Assembly', |
| 13 | + bat: 'Batchfile', |
| 14 | + 'c#': 'C#', |
| 15 | + csharp: 'C#', |
| 16 | + c: 'C', |
| 17 | + 'c++': 'C++', |
| 18 | + cpp: 'C++', |
| 19 | + chpl: 'Chapel', |
| 20 | + coffee: 'CoffeeScript', |
| 21 | + 'coffee-script': 'CoffeeScript', |
| 22 | + cfm: 'ColdFusion', |
| 23 | + 'common-lisp': 'Common Lisp', |
| 24 | + lisp: 'Common Lisp', |
| 25 | + dpatch: 'Darcs Patch', |
| 26 | + dart: 'Dart', |
| 27 | + elisp: 'Emacs Lisp', |
| 28 | + emacs: 'Emacs Lisp', |
| 29 | + 'emacs-lisp': 'Emacs Lisp', |
| 30 | + pot: 'Gettext Catalog', |
| 31 | + html: 'HTML', |
| 32 | + xhtml: 'HTML', |
| 33 | + 'html+erb': 'HTML+ERB', |
| 34 | + erb: 'HTML+ERB', |
| 35 | + irc: 'IRC log', |
| 36 | + json: 'JSON', |
| 37 | + jsp: 'Java Server Pages', |
| 38 | + java: 'Java', |
| 39 | + javascript: 'JavaScript', |
| 40 | + js: 'JavaScript', |
| 41 | + lhs: 'Literate Haskell', |
| 42 | + 'literate-haskell': 'Literate Haskell', |
| 43 | + objc: 'Objective-C', |
| 44 | + openedge: 'OpenEdge ABL', |
| 45 | + progress: 'OpenEdge ABL', |
| 46 | + abl: 'OpenEdge ABL', |
| 47 | + pir: 'Parrot Internal Representation', |
| 48 | + posh: 'PowerShell', |
| 49 | + puppet: 'Puppet', |
| 50 | + 'pure-data': 'Pure Data', |
| 51 | + raw: 'Raw token data', |
| 52 | + rb: 'Ruby', |
| 53 | + ruby: 'Ruby', |
| 54 | + r: 'R', |
| 55 | + scheme: 'Scheme', |
| 56 | + bash: 'Shell', |
| 57 | + sh: 'Shell', |
| 58 | + shell: 'Shell', |
| 59 | + zsh: 'Shell', |
| 60 | + supercollider: 'SuperCollider', |
| 61 | + tex: 'TeX', |
| 62 | + ts: 'TypeScript', |
| 63 | + vim: 'Vim script', |
| 64 | + viml: 'Vim script', |
| 65 | + rst: 'reStructuredText', |
| 66 | + xbm: 'X BitMap', |
| 67 | + xpm: 'X PixMap', |
| 68 | + yaml: 'YAML', |
| 69 | + yml: 'YAML', |
| 70 | + |
| 71 | + // Unofficial languages |
| 72 | + shellsession: 'Shell', |
| 73 | + jsx: 'JSX' |
| 74 | +} |
| 75 | + |
| 76 | +const COPY_REGEX = /\{:copy\}$/ |
| 77 | + |
| 78 | +/** |
| 79 | + * Adds a bar above code blocks that shows the language and a copy button |
| 80 | + */ |
| 81 | +module.exports = function addCodeHeader (node) { |
| 82 | + // Check if the language matches `lang{:copy}` |
| 83 | + const hasCopy = node.lang && COPY_REGEX.test(node.lang) |
| 84 | + |
| 85 | + if (hasCopy) { |
| 86 | + // js{:copy} => js |
| 87 | + node.lang = node.lang.replace(COPY_REGEX, '') |
| 88 | + } else { |
| 89 | + // It doesn't have the copy annotation, so don't add the header |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + // Display the language using the above map of `{ [shortCode]: language }` |
| 94 | + const language = LANGUAGE_MAP[node.lang] || node.lang || 'Code' |
| 95 | + |
| 96 | + const btnIconHtml = octicons.clippy.toSVG() |
| 97 | + const btnIconAst = parse5.parse(String(btnIconHtml)) |
| 98 | + const btnIcon = fromParse5(btnIconAst, btnIconHtml) |
| 99 | + |
| 100 | + // Need to create the header using Markdown AST utilities, to fit |
| 101 | + // into the Unified processor ecosystem. |
| 102 | + const header = h( |
| 103 | + 'header', |
| 104 | + { |
| 105 | + class: [ |
| 106 | + 'd-flex', |
| 107 | + 'flex-items-center', |
| 108 | + 'flex-justify-between', |
| 109 | + 'p-2', |
| 110 | + 'text-small', |
| 111 | + 'rounded-top-1', |
| 112 | + 'border' |
| 113 | + ] |
| 114 | + }, |
| 115 | + [ |
| 116 | + h('span', language), |
| 117 | + h( |
| 118 | + 'button', |
| 119 | + { |
| 120 | + class: [ |
| 121 | + 'js-btn-copy', |
| 122 | + 'btn', |
| 123 | + 'btn-sm', |
| 124 | + 'tooltipped', |
| 125 | + 'tooltipped-nw' |
| 126 | + ], |
| 127 | + 'data-clipboard-text': node.value, |
| 128 | + 'aria-label': 'Copy code to clipboard' |
| 129 | + }, |
| 130 | + btnIcon |
| 131 | + ) |
| 132 | + ] |
| 133 | + ) |
| 134 | + |
| 135 | + return { |
| 136 | + before: [header] |
| 137 | + } |
| 138 | +} |
0 commit comments