Просмотр исходного кода

add consts for RemarkGrowiPluginType

Yuki Takei 3 лет назад
Родитель
Сommit
84210d8894

+ 8 - 8
packages/remark-growi-plugin/readme.md

@@ -137,8 +137,8 @@ function myRemarkPlugin() {
   return (tree) => {
     visit(tree, (node) => {
       if (
-        node.type === 'textGrowiPluginDirective' ||
-        node.type === 'leafGrowiPluginDirective'
+        node.type === DirectiveType.Text ||
+        node.type === DirectiveType.Leaf
       ) {
         const data = node.data || (node.data = {})
         const hast = h(node.name, node.attributes)
@@ -187,8 +187,8 @@ function myRemarkPlugin() {
   return (tree, file) => {
     visit(tree, (node) => {
       if (
-        node.type === 'textGrowiPluginDirective' ||
-        node.type === 'leafGrowiPluginDirective'
+        node.type === DirectiveType.Text ||
+        node.type === DirectiveType.Leaf
       ) {
         if (node.name !== 'youtube') return
 
@@ -196,7 +196,7 @@ function myRemarkPlugin() {
         const attributes = node.attributes || {}
         const id = attributes.id
 
-        if (node.type === 'textGrowiPluginDirective') file.fail('Text directives for `youtube` not supported', node)
+        if (node.type === DirectiveType.Text) file.fail('Text directives for `youtube` not supported', node)
         if (!id) file.fail('Missing video id', node)
 
         data.hName = 'iframe'
@@ -245,13 +245,13 @@ function myRemarkPlugin() {
   return (tree) => {
     visit(tree, (node) => {
       if (
-        node.type === 'textGrowiPluginDirective' ||
-        node.type === 'leafGrowiPluginDirective'
+        node.type === DirectiveType.Text ||
+        node.type === DirectiveType.Leaf
       ) {
         if (node.name !== 'note') return
 
         const data = node.data || (node.data = {})
-        const tagName = node.type === 'textGrowiPluginDirective' ? 'span' : 'div'
+        const tagName = node.type === DirectiveType.Text ? 'span' : 'div'
 
         data.hName = tagName
         data.hProperties = h(tagName, node.attributes).properties

+ 3 - 0
packages/remark-growi-plugin/src/index.js

@@ -1,3 +1,6 @@
+import { DirectiveType } from './mdast-util-growi-plugin/consts.js';
 import { remarkGrowiPlugin } from './remark-growi-plugin.js';
 
+export { DirectiveType as RemarkGrowiPluginType };
+
 export default remarkGrowiPlugin;

+ 7 - 4
packages/remark-growi-plugin/src/mdast-util-growi-plugin/complex-types.d.ts

@@ -1,6 +1,9 @@
 import type { PhrasingContent } from 'mdast';
 import type { Parent } from 'unist';
 
+import { DirectiveType } from './consts.js';
+
+
 type DirectiveAttributes = Record<string, string>
 
 interface DirectiveFields {
@@ -9,21 +12,21 @@ interface DirectiveFields {
 }
 
 export interface TextDirective extends Parent, DirectiveFields {
-  type: 'textGrowiPluginDirective'
+  type: DirectiveType.Text
   children: PhrasingContent[]
 }
 
 export interface LeafDirective extends Parent, DirectiveFields {
-  type: 'leafGrowiPluginDirective'
+  type: DirectiveType.Leaf
   children: PhrasingContent[]
 }
 
 declare module 'mdast' {
   interface StaticPhrasingContentMap {
-    textGrowiPluginDirective: TextDirective
+    [DirectiveType.Text]: TextDirective
   }
 
   interface BlockContentMap {
-    leafGrowiPluginDirective: LeafDirective
+    [DirectiveType.Leaf]: LeafDirective
   }
 }

+ 4 - 0
packages/remark-growi-plugin/src/mdast-util-growi-plugin/consts.js

@@ -0,0 +1,4 @@
+export const DirectiveType = Object.freeze({
+  Text: 'textGrowiPluginDirective',
+  Leaf: 'leafGrowiPluginDirective',
+});

+ 11 - 9
packages/remark-growi-plugin/src/mdast-util-growi-plugin/index.js

@@ -20,6 +20,8 @@ import { track } from 'mdast-util-to-markdown/lib/util/track.js';
 import { parseEntities } from 'parse-entities';
 import { stringifyEntitiesLight } from 'stringify-entities';
 
+import { DirectiveType } from './consts.js';
+
 const own = {}.hasOwnProperty;
 
 const shortcut = /^[^\t\n\r "#'.<=>`}]+$/;
@@ -28,7 +30,7 @@ handleDirective.peek = peekDirective;
 
 /** @type {FromMarkdownExtension} */
 export const directiveFromMarkdown = {
-  canContainEols: ['textGrowiPluginDirective'],
+  canContainEols: [DirectiveType.Text],
   enter: {
     directiveLeaf: enterLeaf,
     directiveLeafAttributes: enterAttributes,
@@ -60,11 +62,11 @@ export const directiveToMarkdown = {
   unsafe: [
     {
       character: '\r',
-      inConstruct: ['leafGrowiPluginDirectiveLabel'],
+      inConstruct: [DirectiveType.Leaf],
     },
     {
       character: '\n',
-      inConstruct: ['leafGrowiPluginDirectiveLabel'],
+      inConstruct: [DirectiveType.Leaf],
     },
     {
       before: '[^$]',
@@ -75,19 +77,19 @@ export const directiveToMarkdown = {
     { atBreak: true, character: '$', after: '$' },
   ],
   handlers: {
-    leafGrowiPluginDirective: handleDirective,
-    textGrowiPluginDirective: handleDirective,
+    [DirectiveType.Leaf]: handleDirective,
+    [DirectiveType.Text]: handleDirective,
   },
 };
 
 /** @type {FromMarkdownHandle} */
 function enterLeaf(token) {
-  enter.call(this, 'leafGrowiPluginDirective', token);
+  enter.call(this, DirectiveType.Leaf, token);
 }
 
 /** @type {FromMarkdownHandle} */
 function enterText(token) {
-  enter.call(this, 'textGrowiPluginDirective', token);
+  enter.call(this, DirectiveType.Text, token);
 }
 
 /**
@@ -228,7 +230,7 @@ function peekDirective() {
  */
 function attributes(node, context) {
   const quote = checkQuote(context);
-  const subset = node.type === 'textGrowiPluginDirective' ? [quote] : [quote, '\n', '\r'];
+  const subset = node.type === DirectiveType.Text ? [quote] : [quote, '\n', '\r'];
   const attrs = node.attributes || {};
   /** @type {Array.<string>} */
   const values = [];
@@ -314,7 +316,7 @@ function attributes(node, context) {
 function fence(node) {
   let size = 0;
 
-  if (node.type === 'leafGrowiPluginDirective') {
+  if (node.type === DirectiveType.Leaf) {
     size = 1;
   }
   else {

+ 5 - 5
packages/remark-growi-plugin/src/mdast-util-growi-plugin/readme.md

@@ -132,7 +132,7 @@ console.log(out)
       children: [
         {type: 'text', value: 'A lovely language know as '},
         {
-          type: 'textGrowiPluginDirective',
+          type: DirectiveType.Text,
           name: 'abbr',
           attributes: {title: 'HyperText Markup Language'},
           children: [{type: 'text', value: 'HTML'}]
@@ -175,7 +175,7 @@ The following interfaces are added to **[mdast][]** by this utility.
 
 ```idl
 interface TextDirective <: Parent {
-  type: 'textGrowiPluginDirective'
+  type: DirectiveType.Text
   children: [PhrasingContent]
 }
 
@@ -197,7 +197,7 @@ Yields:
 
 ```js
 {
-  type: 'textGrowiPluginDirective',
+  type: DirectiveType.Text,
   name: 'name',
   attributes: {id: 'x', class: 'y z', key: 'value'},
   children: [{type: 'text', value: 'Label'}]
@@ -208,7 +208,7 @@ Yields:
 
 ```idl
 interface LeafDirective <: Parent {
-  type: 'leafGrowiPluginDirective'
+  type: DirectiveType.Leaf
   children: [PhrasingContent]
 }
 
@@ -230,7 +230,7 @@ Yields:
 
 ```js
 {
-  type: 'leafGrowiPluginDirective',
+  type: DirectiveType.Leaf,
   name: 'youtube',
   attributes: {v: '123'},
   children: [{type: 'text', value: 'Label'}]

+ 5 - 4
packages/remark-growi-plugin/src/micromark-extension-growi-plugin/lib/html.js

@@ -6,7 +6,6 @@
 
 /**
  * @typedef {[string, string]} Attribute
- * @typedef {'leafGrowiPluginDirective'|'textGrowiPluginDirective'} DirectiveType
  *
  * @typedef Directive
  * @property {DirectiveType} type
@@ -24,6 +23,8 @@
 import { parseEntities } from 'parse-entities';
 import { ok as assert } from 'uvu/assert';
 
+import { DirectiveType } from '../../mdast-util-growi-plugin/consts.js';
+
 const own = {}.hasOwnProperty;
 
 /**
@@ -35,13 +36,13 @@ export function directiveHtml(options = {}) {
     enter: {
 
       directiveLeaf() {
-        return enter.call(this, 'leafGrowiPluginDirective');
+        return enter.call(this, DirectiveType.Leaf);
       },
       directiveLeafAttributes: enterAttributes,
       directiveLeafLabel: enterLabel,
 
       directiveText() {
-        return enter.call(this, 'textGrowiPluginDirective');
+        return enter.call(this, DirectiveType.Text);
       },
       directiveTextAttributes: enterAttributes,
       directiveTextLabel: enterLabel,
@@ -187,7 +188,7 @@ export function directiveHtml(options = {}) {
       found = result !== false;
     }
 
-    if (!found && directive.type !== 'textGrowiPluginDirective') {
+    if (!found && directive.type !== DirectiveType.Text) {
       this.setData('slurpOneLineEnding', true);
     }
   }

+ 2 - 2
packages/remark-growi-plugin/src/micromark-extension-growi-plugin/readme.md

@@ -57,7 +57,7 @@ const output = micromark(fs.readFileSync('example.md'), {
 console.log(output)
 
 function abbr(d) {
-  if (d.type !== 'textGrowiPluginDirective') return false
+  if (d.type !== DirectiveType.Text) return false
 
   this.tag('<abbr')
 
@@ -120,7 +120,7 @@ An object representing a directive.
 
 ###### Fields
 
-*   `type` (`'textGrowiPluginDirective'|'leafGrowiPluginDirective'`)
+*   `type` (`DirectiveType.Text|DirectiveType.Leaf`)
 *   `name` (`string`) — name of directive
 *   `label` (`string?`) — compiled HTML content that was in `[brackets]`
 *   `attributes` (`Record<string, string>?`) — object w/ HTML attributes

+ 25 - 24
packages/remark-growi-plugin/test/mdast-util-growi-plugin.test.js

@@ -3,6 +3,7 @@ import { toMarkdown } from 'mdast-util-to-markdown';
 import test from 'tape';
 import { removePosition } from 'unist-util-remove-position';
 
+import { DirectiveType } from '../src/mdast-util-growi-plugin/consts.js';
 import { directiveFromMarkdown, directiveToMarkdown } from '../src/mdast-util-growi-plugin/index.js';
 import { directive } from '../src/micromark-extension-growi-plugin/index.js';
 
@@ -24,7 +25,7 @@ test('markdown -> mdast', (t) => {
           },
         },
         {
-          type: 'textGrowiPluginDirective',
+          type: DirectiveType.Text,
           name: 'b',
           attributes: { d: '' },
           children: [
@@ -65,7 +66,7 @@ test('markdown -> mdast', (t) => {
       mdastExtensions: [directiveFromMarkdown],
     }).children[0],
     {
-      type: 'leafGrowiPluginDirective',
+      type: DirectiveType.Leaf,
       name: 'a',
       attributes: { c: '' },
       children: [
@@ -102,7 +103,7 @@ test('markdown -> mdast', (t) => {
           children: [
             { type: 'text', value: 'x ' },
             {
-              type: 'textGrowiPluginDirective',
+              type: DirectiveType.Text,
               name: 'a',
               attributes: {},
               children: [
@@ -134,7 +135,7 @@ test('markdown -> mdast', (t) => {
           children: [
             { type: 'text', value: 'x ' },
             {
-              type: 'textGrowiPluginDirective',
+              type: DirectiveType.Text,
               name: 'a',
               attributes: {
                 id: 'b', class: 'c d', e: 'f', g: 'h&i&unknown;j',
@@ -163,7 +164,7 @@ test('markdown -> mdast', (t) => {
           type: 'paragraph',
           children: [
             {
-              type: 'textGrowiPluginDirective',
+              type: DirectiveType.Text,
               name: 'a',
               attributes: { b: '', c: 'd\ne' },
               children: [],
@@ -186,7 +187,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           // @ts-expect-error: `children`, `name` missing.
-          { type: 'textGrowiPluginDirective' },
+          { type: DirectiveType.Text },
           { type: 'text', value: ' b.' },
         ],
       },
@@ -203,7 +204,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           // @ts-expect-error: `children` missing.
-          { type: 'textGrowiPluginDirective', name: 'b' },
+          { type: DirectiveType.Text, name: 'b' },
           { type: 'text', value: ' c.' },
         ],
       },
@@ -220,7 +221,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           {
-            type: 'textGrowiPluginDirective',
+            type: DirectiveType.Text,
             name: 'b',
             children: [{ type: 'text', value: 'c' }],
           },
@@ -240,7 +241,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           {
-            type: 'textGrowiPluginDirective',
+            type: DirectiveType.Text,
             name: 'b',
             children: [{ type: 'text', value: 'c[d]e' }],
           },
@@ -260,7 +261,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           {
-            type: 'textGrowiPluginDirective',
+            type: DirectiveType.Text,
             name: 'b',
             children: [{ type: 'text', value: 'c\nd' }],
           },
@@ -280,7 +281,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           {
-            type: 'textGrowiPluginDirective',
+            type: DirectiveType.Text,
             name: 'b',
             // @ts-expect-error: should contain only `string`s
             attributes: {
@@ -304,7 +305,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           {
-            type: 'textGrowiPluginDirective',
+            type: DirectiveType.Text,
             name: 'b',
             attributes: { class: 'a b\nc', id: 'd', key: 'value' },
             children: [],
@@ -325,7 +326,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           {
-            type: 'textGrowiPluginDirective',
+            type: DirectiveType.Text,
             name: 'b',
             attributes: { x: 'y"\'\r\nz' },
             children: [],
@@ -346,7 +347,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           {
-            type: 'textGrowiPluginDirective',
+            type: DirectiveType.Text,
             name: 'b',
             attributes: { x: 'y"\'\r\nz' },
             children: [],
@@ -367,7 +368,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           {
-            type: 'textGrowiPluginDirective',
+            type: DirectiveType.Text,
             name: 'b',
             attributes: { id: 'c#d' },
             children: [],
@@ -388,7 +389,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           {
-            type: 'textGrowiPluginDirective',
+            type: DirectiveType.Text,
             name: 'b',
             attributes: { class: 'c.d e<f' },
             children: [],
@@ -409,7 +410,7 @@ test('mdast -> markdown', (t) => {
         children: [
           { type: 'text', value: 'a ' },
           {
-            type: 'textGrowiPluginDirective',
+            type: DirectiveType.Text,
             name: 'b',
             attributes: { class: 'c.d e f<g hij' },
             children: [],
@@ -425,7 +426,7 @@ test('mdast -> markdown', (t) => {
 
   t.deepEqual(
     // @ts-expect-error: `children`, `name` missing.
-    toMarkdown({ type: 'leafGrowiPluginDirective' }, { extensions: [directiveToMarkdown] }),
+    toMarkdown({ type: DirectiveType.Leaf }, { extensions: [directiveToMarkdown] }),
     '$\n',
     'should try to serialize a directive (leaf) w/o `name`',
   );
@@ -433,7 +434,7 @@ test('mdast -> markdown', (t) => {
   t.deepEqual(
     toMarkdown(
       // @ts-expect-error: `children` missing.
-      { type: 'leafGrowiPluginDirective', name: 'a' },
+      { type: DirectiveType.Leaf, name: 'a' },
       { extensions: [directiveToMarkdown] },
     ),
     '$a\n',
@@ -443,7 +444,7 @@ test('mdast -> markdown', (t) => {
   t.deepEqual(
     toMarkdown(
       {
-        type: 'leafGrowiPluginDirective',
+        type: DirectiveType.Leaf,
         name: 'a',
         children: [{ type: 'text', value: 'b' }],
       },
@@ -456,7 +457,7 @@ test('mdast -> markdown', (t) => {
   t.deepEqual(
     toMarkdown(
       {
-        type: 'leafGrowiPluginDirective',
+        type: DirectiveType.Leaf,
         name: 'a',
         children: [{ type: 'text', value: 'b' }],
       },
@@ -469,7 +470,7 @@ test('mdast -> markdown', (t) => {
   t.deepEqual(
     toMarkdown(
       {
-        type: 'leafGrowiPluginDirective',
+        type: DirectiveType.Leaf,
         name: 'a',
         children: [{ type: 'text', value: 'b\nc' }],
       },
@@ -482,7 +483,7 @@ test('mdast -> markdown', (t) => {
   t.deepEqual(
     toMarkdown(
       {
-        type: 'leafGrowiPluginDirective',
+        type: DirectiveType.Leaf,
         name: 'a',
         attributes: { id: 'b', class: 'c d', key: 'e\nf' },
         children: [],
@@ -570,7 +571,7 @@ test('mdast -> markdown', (t) => {
       {
         type: 'paragraph',
         children: [
-          { type: 'textGrowiPluginDirective', name: 'red', children: [] },
+          { type: DirectiveType.Text, name: 'red', children: [] },
           { type: 'text', value: '$' },
         ],
       },

+ 2 - 1
packages/remark-growi-plugin/test/micromark-extension-growi-plugin.test.js

@@ -7,6 +7,7 @@ import { htmlVoidElements } from 'html-void-elements';
 import { micromark } from 'micromark';
 import test from 'tape';
 
+import { DirectiveType } from '../src/mdast-util-growi-plugin/consts.js';
 import { directive as syntax, directiveHtml as html } from '../src/micromark-extension-growi-plugin/index.js';
 
 const own = {}.hasOwnProperty;
@@ -973,7 +974,7 @@ test('content', (t) => {
 
 /** @type {Handle} */
 function abbr(d) {
-  if (d.type !== 'textGrowiPluginDirective') return false;
+  if (d.type !== DirectiveType.Text) return false;
 
   this.tag('<abbr');