function markdownitTagToClass(md, mapping = {}) {
  const toArray = (a) => (Array.isArray(a) ? a : [a]);
  const splitWithSpace = (s) => (s ? s.split(' ') : []);

  function parseTokens(tokens) {
    tokens.forEach((token) => {
      if (/(_open$|hr|image)/.test(token.type) && mapping[token.tag]) {
        const orig = splitWithSpace(token.attrGet('class'));
        const addition = toArray(mapping[token.tag]);
        token.attrSet('class', [...orig, ...addition].join(' '));
      }
      if (token.children) {
        parseTokens(token.children);
      }
    });
  }
  md.core.ruler.push('markdownit-tag-to-class', (state) =>
    parseTokens(state.tokens)
  );
}
Markdown plugin
Add classes to markdown elements by default