|
| 1 | +import {MarkdownItPluginCb} from '../typings'; |
| 2 | +import Core from 'markdown-it/lib/parser_core'; |
| 3 | +import Token from 'markdown-it/lib/token'; |
| 4 | +import {bold} from 'chalk'; |
| 5 | +import yaml from 'js-yaml'; |
| 6 | +import StateCore from 'markdown-it/lib/rules_core/state_core'; |
| 7 | + |
| 8 | +interface Metadata { |
| 9 | + date: string; |
| 10 | +} |
| 11 | + |
| 12 | +interface Options { |
| 13 | + extractChangelogs?: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +const CHANGELOG_OPEN_RE = /^\{% changelog %}/; |
| 17 | +const CHANGELOG_CLOSE_RE = /^\{% endchangelog %}/; |
| 18 | + |
| 19 | +function isOpenToken(tokens: Token[], i: number) { |
| 20 | + return ( |
| 21 | + tokens[i].type === 'paragraph_open' && |
| 22 | + tokens[i + 1].type === 'inline' && |
| 23 | + tokens[i + 2].type === 'paragraph_close' && |
| 24 | + CHANGELOG_OPEN_RE.test(tokens[i + 1].content) |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +function isCloseToken(tokens: Token[], i: number) { |
| 29 | + return ( |
| 30 | + tokens[i]?.type === 'paragraph_open' && |
| 31 | + tokens[i + 1].type === 'inline' && |
| 32 | + tokens[i + 2].type === 'paragraph_close' && |
| 33 | + CHANGELOG_CLOSE_RE.test(tokens[i + 1].content) |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +function isTitle(tokens: Token[], i = 0) { |
| 38 | + return ( |
| 39 | + tokens[i].type === 'heading_open' && |
| 40 | + tokens[i + 1].type === 'inline' && |
| 41 | + tokens[i + 2].type === 'heading_close' |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +function isImageParagraph(tokens: Token[], i = 0) { |
| 46 | + return ( |
| 47 | + tokens[i].type === 'paragraph_open' && |
| 48 | + tokens[i + 1].type === 'inline' && |
| 49 | + tokens[i + 2].type === 'paragraph_close' && |
| 50 | + tokens[i + 1].children?.some((t) => t.type === 'image') |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +function parseBody(tokens: Token[], state: StateCore) { |
| 55 | + const {md, env} = state; |
| 56 | + |
| 57 | + const metadataToken = tokens.shift(); |
| 58 | + if (metadataToken?.type !== 'fence') { |
| 59 | + throw new Error('Metadata tag not found'); |
| 60 | + } |
| 61 | + const rawMetadata = yaml.load(metadataToken.content) as Metadata; |
| 62 | + const metadata = { |
| 63 | + ...rawMetadata, |
| 64 | + date: new Date(rawMetadata.date).toISOString(), |
| 65 | + }; |
| 66 | + |
| 67 | + if (!isTitle(tokens)) { |
| 68 | + throw new Error('Title tag not found'); |
| 69 | + } |
| 70 | + const title = tokens.splice(0, 3)[1].content; |
| 71 | + |
| 72 | + let image; |
| 73 | + if (isImageParagraph(tokens)) { |
| 74 | + const paragraphTokens = tokens.splice(0, 3); |
| 75 | + const imageToken = paragraphTokens[1]?.children?.find((token) => token.type === 'image'); |
| 76 | + if (imageToken) { |
| 77 | + const width = Number(imageToken.attrGet('width')); |
| 78 | + const height = Number(imageToken.attrGet('height')); |
| 79 | + let ratio; |
| 80 | + if (Number.isFinite(width) && Number.isFinite(height)) { |
| 81 | + ratio = height / width; |
| 82 | + } |
| 83 | + let alt = imageToken.attrGet('title') || ''; |
| 84 | + if (!alt && imageToken.children) { |
| 85 | + alt = md.renderer.renderInlineAsText(imageToken.children, md.options, env); |
| 86 | + } |
| 87 | + image = { |
| 88 | + src: imageToken.attrGet('src'), |
| 89 | + alt, |
| 90 | + ratio, |
| 91 | + }; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + const description = md.renderer.render(tokens, md.options, env); |
| 96 | + |
| 97 | + return { |
| 98 | + ...metadata, |
| 99 | + title, |
| 100 | + image, |
| 101 | + description, |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +const changelog: MarkdownItPluginCb<Options> = function (md, {extractChangelogs, log, path}) { |
| 106 | + const plugin: Core.RuleCore = (state) => { |
| 107 | + const {tokens, env} = state; |
| 108 | + |
| 109 | + for (let i = 0, len = tokens.length; i < len; i++) { |
| 110 | + const isOpen = isOpenToken(tokens, i); |
| 111 | + if (!isOpen) continue; |
| 112 | + |
| 113 | + const openAt = i; |
| 114 | + let isCloseFound = false; |
| 115 | + while (i < len) { |
| 116 | + i++; |
| 117 | + if (isCloseToken(tokens, i)) { |
| 118 | + isCloseFound = true; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (!isCloseFound) { |
| 124 | + log.error(`Changelog close tag in not found: ${bold(path)}`); |
| 125 | + break; |
| 126 | + } |
| 127 | + |
| 128 | + const closeAt = i + 2; |
| 129 | + |
| 130 | + if (env && extractChangelogs) { |
| 131 | + const content = tokens.slice(openAt, closeAt + 1); |
| 132 | + |
| 133 | + // cut open |
| 134 | + content.splice(0, 3); |
| 135 | + // cut close |
| 136 | + content.splice(-3); |
| 137 | + |
| 138 | + try { |
| 139 | + const change = parseBody(content, state); |
| 140 | + |
| 141 | + if (!env.changelogs) { |
| 142 | + env.changelogs = []; |
| 143 | + } |
| 144 | + |
| 145 | + env.changelogs.push(change); |
| 146 | + } catch (err) { |
| 147 | + log.error(`Changelog error: ${(err as Error).message} in ${bold(path)}`); |
| 148 | + continue; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + tokens.splice(openAt, closeAt + 1 - openAt); |
| 153 | + len = tokens.length; |
| 154 | + i = openAt - 1; |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + try { |
| 159 | + md.core.ruler.before('curly_attributes', 'changelog', plugin); |
| 160 | + } catch (e) { |
| 161 | + md.core.ruler.push('changelog', plugin); |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | +export = changelog; |
0 commit comments