Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Document OpenProject::TextFormatting

Alexander Brandon Coles8748ab6e09e8
diff --git a/lib/open_project/text_formatting.rb b/lib/open_project/text_formatting.rb
index 2c2bcaaaf94..8147e6f712b 100644
--- a/lib/open_project/text_formatting.rb
+++ b/lib/open_project/text_formatting.rb
@@ -27,13 +27,55 @@
#++
module OpenProject
+ # This module provides high-level text formatting functionality.
+ #
+ # @!method request
+ # Expected to be defined in the including class.
+ # @return [ActionDispatch::Request] the current request context.
+ #
+ # @note
+ # The including class should implement {#request} if {#format_text} is
+ # called within a request cycle.
module TextFormatting
include ::OpenProject::TextFormatting::Truncation
- # Formats text according to system settings.
- # 2 ways to call this method:
- # * with a String: format_text(text, options)
- # * with an object and one of its attribute: format_text(issue, :description, options)
+ # @!macro format_text_options
+ # @param [Hash] options a customizable set of options.
+ # @option options [Project] :project (@project, object#project)
+ # a Project context.
+ # @option options [Boolean] :only_path (true)
+ # whether to generate links with relative URLs.
+ # @option options [User] :current_user (User.current)
+ # the current user context.
+ # @option options [:plain, :rich] :format (:rich)
+ # the text format.
+ # `:plain` will return plain text.
+ # `:rich` will render raw Markdown as HTML.
+
+ ##
+ # Formats text according to system settings and provided options.
+ #
+ # @overload format_text(text, options = {})
+ # @param [String] text the raw text to be formatted, typically Markdown.
+ # @macro format_text_options
+ # @option options [Object] :object an object context.
+ #
+ # @example Setting a project context explicitly
+ # format_text("## Hello world", project: current_project)
+ # @example Generating links with full URLs
+ # format_text("[Projects](/projects)", only_path: false)
+ #
+ # @overload format_text(object, attribute, options = {})
+ # @param [Object] object an object, typically a model
+ # (i.e. `ActiveRecord::Base` descendent).
+ # @param [Symbol] attribute the method on that object.
+ # `#to_s` will be called on the return value.
+ # @macro format_text_options
+ #
+ # @example
+ # format_text(issue, :description, options)
+ #
+ # @return [String] the formatted text as an HTML-safe String.
def format_text(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
case args.size