0
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?

GitBucket Markdown Enhanced Plugin でも issue リンクが使えるようになりました

0
Posted at

概要

GitBucket用のプラグイン GitBucket Markdown Enhanced Pluginを開発しています。

今回は、標準機能にある #123 のように issue 番号を書くことで issue へのリンクが使えるように修正した記録です。

修正するにあたって flexmark-java の拡張機能 GfmIssuesExtension を利用しました。

前回の記事

時系列は前後しますが…

今回修正したソース

修正前のソース

src\main\scala\io\github\yasumichi\gme\MarkdownEnhancedRenderer.scala

修正したコミット

Enable Issue link · yasumichi/gitbucket-markdown-enhanced@c18a93f

GfmIssuesExtension の有効化

Parser.EXTENSIONS に設定する拡張機能のシーケンスに GfmIssuesExtension.create() を追加しました。

    val extension: Seq[Extension] = Seq(
      ...
      GfmIssuesExtension.create(),
      ...
    )

GIT_HUB_ISSUES_URL_ROOT の設定

リンク先の URL が、GitBucket の URL になるように以下のソースを追加しました。

    // Determine current path for GitHub Issues links
    var currentPath = ""
    if (context.currentPath.endsWith("_preview")) {
      currentPath = context.request.getHeader("Referer").substring(context.baseUrl.length)
    } else {
      currentPath = context.currentPath
    } 

    val pathElems = currentPath.split("/")
    if (pathElems.length > 2 && pathElems(1) != "admin") {
      var owner = pathElems(1)
      var repos = pathElems(2)

      options.set(GfmIssuesExtension.GIT_HUB_ISSUES_URL_ROOT, context.baseUrl + "/" + owner + "/" + repos + "/issues") 
    }

以下、分けて説明します。

プラグインを呼び出した URL から GitBucket の基本 URL を取り除いた部分を取得する

プレビュー画面から呼び出された場合、どの機能から呼び出されたか分からないため、リクエストの Referer からパスを取得するようにしていました。

    var currentPath = ""
    if (context.currentPath.endsWith("_preview")) {
      currentPath = context.request.getHeader("Referer").substring(context.baseUrl.length)
    } else {
      currentPath = context.currentPath
    } 

当初は、機能ごとに処理を分けるつもりでしたが、今考えるとこのコード必要ありません。次の機会に削除します。

※issues と Pull Requests で処理を分ける必要があると思っていましたが、issues に Pull Request の ID でアクセスすると Pull Requests の URL にリダイレクトされるのですべて issues の URL を設定しておけば問題ありません。

最終的に /user/repository/function のようなパスが取得できます。

currentPath からユーザー名とリポジトリ名を取得し issues の URL を構成

    val pathElems = currentPath.split("/")

currentPath を分解し、配列にします。/user/repository/function であれば、以下のように分割されます。

[0] [1] [2] [3]
空文字列 user repository function
    if (pathElems.length > 2 && pathElems(1) != "admin") {

念のため、以下のチェックをします。

  • 最低限、ユーザー名とリポジトリ名まで含まれていること。(配列の長さが 3 以上であること。)
  • System administration の画面ではないこと
      var owner = pathElems(1)
      var repos = pathElems(2)

      options.set(GfmIssuesExtension.GIT_HUB_ISSUES_URL_ROOT, context.baseUrl + "/" + owner + "/" + repos + "/issues") 

ユーザー名とリポジトリ名から、issues の URL が確定するので GIT_HUB_ISSUES_URL_ROOT を設定します。

プラグインの入手先

Releases · yasumichi/gitbucket-markdown-enhanced から、最新バージョンをダウンロードしてください。

0
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
0
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?