1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Cloudflare Access で保護された Confluence に Markdown をアップロードする

Posted at

目的

一部変更した md_to_conf ツールを使って以下のような構成を実現します。

Confluence に Markdown をアップロードするには

以下の md_to_conf ツールが使えます。

以下の記事が参考になります。

マクロや HTML タグの変換を支援してくれる機能や不要なタグによるトラブルシューティングに関する記載があります。

Cloudflare Access で保護された Confluence への対応

Cloudflare Access で保護されている場合、 同一ドメイン配下にある Confluence の API を経由して操作するためには cloudflared によって取得したアクセストークンを cf-access-token ヘッダとしてリクエストに付与する必要があります。

そのため、 md2conf.pyの コードを以下のように変更します。

-       session.headers.update({'Authorization': 'Bearer ' + PA_TOKEN})
+       session.headers.update({'cf-access-token': '' + PA_TOKEN})

その後、以下のような手順で Confluence に Markdown をアップロードできます。

export CONFLUENCE_ORGNAME='your.confluence.com'

cloudflared access login https://$CONFLUENCE_ORGNAME > /dev/null         

export CONFLUENCE_PERSONAL_ACCESS_TOKEN=$(cloudflared access token -app=https://${CONFLUENCE_ORGNAME})

python3 ~/md_to_conf/md2conf.py YOUR_MARKDOWN_FILE_FULLPATH.md '~USERNAME' -l DEBUG

Markdown ファイルの取り扱い

Markdown ファイルの 1 行目が Confluence ページのタイトルとして扱われます。
または --title オプションで指定可能です。

md2conf.py
...
PARSER.add_argument('--title', action='store', dest='title', default=None,
                    help='Set the title for the page, otherwise the title is going to be the first line in the markdown file')
...

Markdown ファイル名は、あくまでローカルファイルの指定のみに使用します。

sample.md
Markdown Upload Test

# Test

Test

| a    | b    | c    |
| ---- | ---- | ---- |
| d    | e    | f    |
| g    | h    | i    |
| j    | k    | l    |

既存ページタイトルにマッチしなかった場合

新規ページが作成されます。

image.png

既存ページタイトルにマッチする場合

指定の階層にアップロードしたい場合は、事前にページを作っておくことで、既存ページタイトルにマッチした場所へアップロードできます。

image.png

参考:その他の変換

以下の要領で Confluence ページの見やすいマクロに変換する処理を追加で実装できます。

ステータスマクロ

status.md
| ROADMAP | SPEC | BLOG | BUG  | APPROVED |
| ------- | ---- | ---- | ---- | -------- |
| ROADMAP | SPEC | BLOG | BUG  | APPROVED |

image.png

+    # ROADMAP Status Macro Converter
+    statuses = re.findall(r'<td>ROADMAP</td>', html)
+    if statuses:
+        for status in statuses:
+            html = html.replace(
+                status, '<td><ac:structured-macro ac:name="status" ac:schema-version="1"><ac:parameter ac:name="colour">Yellow</ac:parameter><ac:parameter ac:name="title">ROADMAP</ac:parameter></ac:structured-macro></td>')

絵文字マクロ

emotion.md
| GOING  | WAIT      | DONE               |
| ------ | --------- | ------------------ |
| :star: | :pushpin: | :white_check_mark: |

image.png

+    # DONE Status Macro Converter
+    statuses = re.findall(r'<td>:white_check_mark:</td>', html)
+    if statuses:
+        for status in statuses:
+            html = html.replace(
+                status, '<td><ac:emoticon ac:name="tick"/></td>')

JIRA マクロ

jira.md
| JIRA                                                         |
| ------------------------------------------------------------ |
| [[xxx-####] Kyouhei-test - Jira](https://jira.confluence.com/browse/xxx-####) |

image.png

+    # Jira Macro Converter
+    links = re.findall(r'<a href=".+?">.+?</a>', html)
+    if links:
+        for link in links:
+            matches = re.search(r'<a href="(.+?)">(.+?)</a>', link)
+            ref = matches.group(1)
+            alt = matches.group(2)
+            jira_url = "jira." + '.'.join(ORGNAME.split('.')[-2:])
+            if urllib.parse.urlparse(ref).hostname == jira_url:
+                key = ref.rsplit('/', 1)[-1]
+                html = html.replace(
+                    link, '<ac:structured-macro ac:name="jiraissues"><ac:parameter ac:name="anonymous">true</ac:parameter><ac:parameter ac:name="key">%s</ac:parameter></ac:structured-macro>' % key)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?