Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: wiki - add toc to wiki pages and rendered md files #7182

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6325510
add toc to wikipages and rendered md files
Cherrg Jun 11, 2019
0a9fb89
move style settings from js to css files
Cherrg Jun 11, 2019
0e08aa9
fix dark theme border color
Cherrg Jun 11, 2019
994bf33
change padding on TOC headlines
Cherrg Jun 12, 2019
f60056b
add md and wikifile TOC settings to repository options
Cherrg Jun 12, 2019
a9dce7f
handle options on template and update js
Cherrg Jun 12, 2019
e971230
Update index.js
Cherrg Jun 13, 2019
a889262
add float clear element
Cherrg Jun 13, 2019
77fb621
move TOC settings in app.ini from section 'repository' to 'markdown'
Cherrg Jun 13, 2019
c8e7b8a
Merge branch 'master' into gitea_md_wiki_auto_toc
techknowlogick Jun 13, 2019
06238aa
remove german translation
Cherrg Jun 13, 2019
c486aca
add configuration flag to set TOC extensions
Cherrg Jun 13, 2019
1417170
Merge branch 'master' into gitea_md_wiki_auto_toc
Cherrg Jun 13, 2019
83e4c6e
Merge branch 'gitea_md_wiki_auto_toc' of github.com:Cherrg/gitea into…
Cherrg Jun 13, 2019
588fcca
move toc beside markdown container on large screens
Cherrg Jun 13, 2019
e91b8c5
rename configuration variables 'markdown' to 'markup'
Cherrg Jun 14, 2019
8dd08af
set fixed width on small mobile screens
Cherrg Jun 17, 2019
71e0165
Merge branch 'master' into gitea_md_wiki_auto_toc
Cherrg Jun 22, 2019
21b1f99
Merge branch 'master' into gitea_md_wiki_auto_toc
Cherrg Jun 27, 2019
22b82ff
fix css lint errors
Cherrg Jun 27, 2019
0607c0a
rename migration file
Cherrg Jul 6, 2019
e0939ab
Merge branch 'master' into gitea_md_wiki_auto_toc
Cherrg Jul 6, 2019
bd83992
git rename migration file to resolve confilcts
Cherrg Jul 8, 2019
9fbb263
Merge branch 'master' into gitea_md_wiki_auto_toc
Cherrg Jul 8, 2019
f870a41
Merge branch 'master' into gitea_md_wiki_auto_toc
Cherrg Nov 16, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ CUSTOM_URL_SCHEMES =
; List of file extensions that should be rendered/edited as Markdown
; Separate the extensions with a comma. To render files without any extension as markdown, just put a comma
FILE_EXTENSIONS = .md,.markdown,.mdown,.mkd
; Create Table of Contents based on headlines on wiki page (does not alter file)
DEFAULT_TOC_WIKI_FILE = true
; Create 'Table of Contents' on all rendered markup files. This does not alter files. Enabled: overwrites DEFAULT_TOC_MARKUP_BY_FLAG
DEFAULT_TOC_MARKUP_ALWAYS = false
; Create 'Table of Contents' on rendered markup files if line '%%TOC%%' is present.
DEFAULT_TOC_MARKUP_BY_FLAG = true
; List of markup file extensions that TOC should be created on, set to not interfere with external markup renderer.
; Separate the extensions with a comma. To ignore file extension check, just put a comma.
TOC_MARKUP_FILE_EXTENSIONS = .md,.markdown,.mdown,.mkd,.org

[server]
; The protocol the server listens on. One of 'http', 'https', 'unix' or 'fcgi'.
Expand Down
6 changes: 6 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
## Markdown (`markdown`)

- `ENABLE_HARD_LINE_BREAK`: **false**: Enable Markdown's hard line break extension.
- `DEFAULT_TOC_WIKI_FILE`: **true**: Create Table of Contents based on headlines on wiki page (does
not alter file)
- `DEFAULT_TOC_MARKUP_ALWAYS`: **false** : Create 'Table of Contents' on all rendered markup files. This does not alter files. Enabled: overwrites `DEFAULT_TOC_MARKUP_BY_FLAG`
- `DEFAULT_TOC_MARKUP_BY_FLAG`: **true** : Create 'Table of Contents' on rendered markup files if line `%%TOC%%` is present.
- `TOC_MARKUP_FILE_EXTENSIONS`: **.md,.markdown,.mdown,.mkd,.org** : List of markup file extensions that TOC should be created on, set to not interfere with external markup renderer.
Separate the extensions with a comma. To ignore file extension check, just put a comma.
- `CUSTOM_URL_SCHEMES`: Use a comma separated list (ftp,git,svn) to indicate additional
URL hyperlinks to be rendered in Markdown. URLs beginning in http and https are
always displayed
Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ var migrations = []Migration{
NewMigration("Add template options to repository", addTemplateToRepo),
// v108 -> v109
NewMigration("Add comment_id on table notification", addCommentIDOnNotification),
// v109 -> v110
NewMigration("add toc on wiki and markdown", addCanTocOnWikiAndMarkdown),
}

// Migrate database to current version
Expand Down
40 changes: 40 additions & 0 deletions models/migrations/v109.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"code.gitea.io/gitea/modules/setting"

"xorm.io/xorm"
)

func addCanTocOnWikiAndMarkdown(x *xorm.Engine) error {

type Repository struct {
TocWikiFile bool `xorm:"NOT NULL DEFAULT true"`
TocMarkupAlways bool `xorm:"NOT NULL DEFAULT false"`
TocMarkupByFlag bool `xorm:"NOT NULL DEFAULT true"`
}

if err := x.Sync2(new(Repository)); err != nil {
return err
}

if _, err := x.Exec("UPDATE repository SET toc_wiki_file = ?",
setting.Markdown.DefaultTocWikiFile); err != nil {
return err
}

if _, err := x.Exec("UPDATE repository SET toc_markup_always = ?",
setting.Markdown.DefaultTocMarkupAlways); err != nil {
return err
}

if _, err := x.Exec("UPDATE repository SET toc_markup_by_flag = ?",
setting.Markdown.DefaultTocMarkupByFlag); err != nil {
return err
}
return nil
}
6 changes: 6 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ type Repository struct {
IndexerStatus *RepoIndexerStatus `xorm:"-"`
IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"`
CloseIssuesViaCommitInAnyBranch bool `xorm:"NOT NULL DEFAULT false"`
TocWikiFile bool `xorm:"NOT NULL DEFAULT true"`
TocMarkupAlways bool `xorm:"NOT NULL DEFAULT false"`
TocMarkupByFlag bool `xorm:"NOT NULL DEFAULT true"`
Topics []string `xorm:"TEXT JSON"`

// Avatar: ID(10-20)-md5(32) - must fit into 64 symbols
Expand Down Expand Up @@ -1620,6 +1623,9 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err
IsPrivate: opts.IsPrivate,
IsFsckEnabled: !opts.IsMirror,
CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch,
TocWikiFile: setting.Markdown.DefaultTocWikiFile,
TocMarkupAlways: setting.Markdown.DefaultTocMarkupAlways,
TocMarkupByFlag: setting.Markdown.DefaultTocMarkupByFlag,
Status: opts.Status,
IsEmpty: !opts.AutoInit,
}
Expand Down
3 changes: 3 additions & 0 deletions modules/auth/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ type RepoSettingForm struct {
AllowOnlyContributorsToTrackTime bool
EnableIssueDependencies bool
IsArchived bool
TocWikiFile bool
TocMarkupAlways bool
TocMarkupByFlag bool

// Admin settings
EnableHealthCheck bool
Expand Down
18 changes: 13 additions & 5 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,20 @@ var (

// Markdown settings
Markdown = struct {
EnableHardLineBreak bool
CustomURLSchemes []string `ini:"CUSTOM_URL_SCHEMES"`
FileExtensions []string
EnableHardLineBreak bool
CustomURLSchemes []string `ini:"CUSTOM_URL_SCHEMES"`
FileExtensions []string
DefaultTocWikiFile bool
DefaultTocMarkupAlways bool
DefaultTocMarkupByFlag bool
TocMarkupFileExtensions []string
}{
EnableHardLineBreak: false,
FileExtensions: strings.Split(".md,.markdown,.mdown,.mkd", ","),
EnableHardLineBreak: false,
FileExtensions: strings.Split(".md,.markdown,.mdown,.mkd", ","),
DefaultTocWikiFile: true,
DefaultTocMarkupAlways: false,
DefaultTocMarkupByFlag: true,
TocMarkupFileExtensions: strings.Split(".md,.markdown,.mdown,.mkd,.org", ","),
}

// Admin settings
Expand Down
4 changes: 4 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,10 @@ settings.pulls.allow_squash_commits = Enable Squashing to Merge Commits
settings.admin_settings = Administrator Settings
settings.admin_enable_health_check = Enable Repository Health Checks (git fsck)
settings.admin_enable_close_issues_via_commit_in_any_branch = Close an issue via a commit made in a non default branch
settings.toc.toc = Table of Contents
settings.toc.toc_wiki_file = Enable autogenerated TOC on wikipages (based on headlines) (does not alter files)
settings.toc.toc_markup_always = Enable autogenerated TOC on all markup files (based on headlines) (does not alter files)
settings.toc.toc_markup_by_flag = Enable autogenerated TOC on markup files which contain keyword '%%TOC%%' (based on headlines) (does not alter files)
settings.danger_zone = Danger Zone
settings.new_owner_has_same_repo = The new owner already has a repository with same name. Please choose another name.
settings.convert = Convert to Regular Repository
Expand Down
10 changes: 9 additions & 1 deletion public/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ i.icon.centerlock{top:1.5em}
.code-view :not(.fa):not(.octicon):not(.icon){font-size:12px;font-family:'SF Mono',Consolas,Menlo,'Liberation Mono',Monaco,'Lucida Console',monospace;line-height:20px}
.code-view table{width:100%}
.code-view .active{background:#fff866}
.markdown:not(code){overflow:hidden;font-size:16px;line-height:1.6!important;word-wrap:break-word}
.markdown:not(code){font-size:16px;line-height:1.6!important;word-wrap:break-word}
.markdown:not(code).ui.segment{padding:3em}
.markdown:not(code).file-view{padding:2em 2em 2em!important}
.markdown:not(code)>:first-child{margin-top:0!important}
Expand Down Expand Up @@ -329,6 +329,14 @@ i.icon.centerlock{top:1.5em}
.markdown:not(code) .csv-data tr{border-top:0}
.markdown:not(code) .csv-data th{font-weight:700;background:#f8f8f8;border-top:0}
.markdown:not(code) .ui.list .list,.markdown:not(code) ol.ui.list ol,.markdown:not(code) ul.ui.list ul{padding-left:2em}
.markdown:not(code) .auto-toc-wrapper{position:relative;padding:0 0 7px 20px;float:right;background:#fff;z-index:1}
@media only screen and (min-width:1760px){.markdown:not(code) .auto-toc-wrapper{right:-31%;width:26%;padding:0;margin-top:-3em!important;margin-left:-26%}
}
@media only screen and (max-width:479px){.markdown:not(code) .auto-toc-wrapper{float:none;width:100%;padding:0;margin-bottom:1em}
}
.markdown:not(code) .auto-toc-container{padding:7px;border:1px solid #d4d4d5;border-radius:5px}
.markdown:not(code) .auto-toc-container h2{padding:.3em;font-size:1.65em}
.markdown:not(code) .auto-toc-clear{clear:both;margin-bottom:-20px!important}
.repository.wiki.revisions .ui.container>.ui.stackable.grid{flex-direction:row-reverse}
.repository.wiki.revisions .ui.container>.ui.stackable.grid>.header{margin-top:0}
.repository.wiki.revisions .ui.container>.ui.stackable.grid>.header .sub.header{padding-left:52px;word-break:break-word}
Expand Down
2 changes: 2 additions & 0 deletions public/css/theme-arc-green.css
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ footer{background:#2e323e;border-top:1px solid #313131}
.repository.file.editor.edit,.repository.wiki.new .CodeMirror{border-right:1px solid rgba(187,187,187,.6);border-left:1px solid rgba(187,187,187,.6);border-bottom:1px solid rgba(187,187,187,.6)}
.repository.file.editor.edit .editor-preview,.repository.file.editor.edit .editor-preview-side,.repository.file.editor.edit+.editor-preview-side,.repository.wiki.new .CodeMirror .editor-preview,.repository.wiki.new .CodeMirror .editor-preview-side,.repository.wiki.new .CodeMirror+.editor-preview-side{background:#353945}
.repository.file.editor.edit .editor-preview .markdown:not(code).ui.segment,.repository.file.editor.edit .editor-preview-side .markdown:not(code).ui.segment,.repository.file.editor.edit+.editor-preview-side .markdown:not(code).ui.segment,.repository.wiki.new .CodeMirror .editor-preview .markdown:not(code).ui.segment,.repository.wiki.new .CodeMirror .editor-preview-side .markdown:not(code).ui.segment,.repository.wiki.new .CodeMirror+.editor-preview-side .markdown:not(code).ui.segment{border-width:0}
.markdown:not(code) .auto-toc-wrapper{background:#353945}
.markdown:not(code) .auto-toc-container{background:#2a2e3a;border-color:#404552}
.ui.dropdown .menu{background:#2c303a}
.ui.dropdown .menu>.message:not(.ui){color:#636363}
.ui.input{color:#dbdbdb}
Expand Down
2 changes: 1 addition & 1 deletion public/js/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/index.js.map

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions routers/repo/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,24 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
ctx.ServerError("UpdateRepositoryUnits", err)
return
}

if repo.TocWikiFile != form.TocWikiFile {
repo.TocWikiFile = form.TocWikiFile
}

if repo.TocMarkupAlways != form.TocMarkupAlways {
repo.TocMarkupAlways = form.TocMarkupAlways
}

if repo.TocMarkupByFlag != form.TocMarkupByFlag {
repo.TocMarkupByFlag = form.TocMarkupByFlag
}

if err := models.UpdateRepository(repo, false); err != nil {
ctx.ServerError("UpdateRepository", err)
return
}

log.Trace("Repository advanced settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)

ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
Expand Down
36 changes: 36 additions & 0 deletions routers/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io/ioutil"
"net/url"
"path"
"path/filepath"
"strings"

"code.gitea.io/gitea/models"
Expand Down Expand Up @@ -165,6 +166,23 @@ func renderDirectory(ctx *context.Context, treeLink string) {
buf = charset.ToUTF8WithFallback(append(buf, d...))

if markupType := markup.Type(readmeFile.Name()); markupType != "" {
// Check if extension matches TOC file list
tocExts := setting.Markdown.TocMarkupFileExtensions
isTocMarkup := false
if len(tocExts) == 0 || len(tocExts) == 1 && tocExts[0] == "" || len(tocExts) == 2 && tocExts[0] == "" && tocExts[1] == "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the check be in setting parsing?

isTocMarkup = true
} else {
fileExt := strings.ToLower(filepath.Ext(readmeFile.Name()))
if fileExt != "" {
for _, tExt := range tocExts {
if tExt == fileExt {
isTocMarkup = true
break
}
}
}
}
ctx.Data["IsTocMarkup"] = isTocMarkup
ctx.Data["IsMarkup"] = true
ctx.Data["MarkupType"] = string(markupType)
ctx.Data["FileContent"] = string(markup.Render(readmeFile.Name(), buf, treeLink, ctx.Repo.Repository.ComposeMetas()))
Expand Down Expand Up @@ -296,7 +314,25 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st

readmeExist := markup.IsReadmeFile(blob.Name())
ctx.Data["ReadmeExist"] = readmeExist

if markupType := markup.Type(blob.Name()); markupType != "" {
// Check if extension matches TOC file list
tocExts := setting.Markdown.TocMarkupFileExtensions
isTocMarkup := false
if len(tocExts) == 0 || len(tocExts) == 1 && tocExts[0] == "" || len(tocExts) == 2 && tocExts[0] == "" && tocExts[1] == "" {
isTocMarkup = true
} else {
fileExt := strings.ToLower(filepath.Ext(blob.Name()))
if fileExt != "" {
for _, tExt := range tocExts {
if tExt == fileExt {
isTocMarkup = true
break
}
}
}
}
ctx.Data["IsTocMarkup"] = isTocMarkup
ctx.Data["IsMarkup"] = true
ctx.Data["MarkupType"] = markupType
ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
Expand Down
25 changes: 25 additions & 0 deletions templates/repo/settings/options.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,31 @@
</div>
{{end}}

<div class="ui divider"></div>

<div class="toc_box">
<div class="field">
<label>{{.i18n.Tr "repo.settings.toc.toc"}}</label>
<div class="field">
<div class="ui checkbox">
<input name="toc_wiki_file" type="checkbox" {{if (.Repository.TocWikiFile)}}checked{{end}}>
<label>{{.i18n.Tr "repo.settings.toc.toc_wiki_file"}}</label>
</div>
</div>
<div class="field">
<div class="ui checkbox">
<input name="toc_markup_always" type="checkbox" {{if (.Repository.TocMarkupAlways)}}checked{{end}}>
<label>{{.i18n.Tr "repo.settings.toc.toc_markup_always"}}</label>
</div>
</div>
<div class="field">
<div class="ui checkbox">
<input name="toc_markup_by_flag" type="checkbox" {{if (.Repository.TocMarkupByFlag)}}checked{{end}}>
<label>{{.i18n.Tr "repo.settings.toc.toc_markup_by_flag"}}</label>
</div>
</div>
</div>
</div>
<div class="ui divider"></div>
<div class="field">
<button class="ui green button">{{$.i18n.Tr "repo.settings.update_settings"}}</button>
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/view_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{{end}}
</h4>
<div class="ui attached table unstackable segment">
<div class="file-view {{if .IsMarkup}}{{.MarkupType}} markdown{{else if .IsRenderedHTML}}plain-text{{else if .IsTextFile}}code-view{{end}} has-emoji">
<div class="file-view {{if .IsMarkup}}{{.MarkupType}} markdown{{if and .IsTocMarkup .Repository.TocMarkupAlways}} auto-toc{{else if and .IsTocMarkup .Repository.TocMarkupByFlag}} auto-toc-by-flag{{end}}{{else if .IsRenderedHTML}}plain-text{{else if .IsTextFile}}code-view{{end}} has-emoji"{{if or .Repository.TocMarkupAlways .Repository.TocMarkupByFlag}} data-toc="{{.i18n.Tr "repo.settings.toc.toc"}}"{{end}}>
{{if .IsMarkup}}
{{if .FileContent}}{{.FileContent | Safe}}{{end}}
{{else if .IsRenderedHTML}}
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/wiki/view.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
</div>
{{end}}
<div class="ui {{if .sidebarPresent}}grid equal width{{end}}" style="margin-top: 1rem;">
<div class="ui {{if .sidebarPresent}}eleven wide column{{end}} segment markdown has-emoji">
<div class="ui {{if .sidebarPresent}}eleven wide column{{end}} segment markdown has-emoji{{if (.Repository.TocWikiFile)}} auto-toc{{end}}"{{if .Repository.TocWikiFile}} data-toc="{{.i18n.Tr "repo.settings.toc.toc"}}"{{end}}>
{{.content | Str2html}}
</div>
{{if .sidebarPresent}}
Expand Down
Loading