Skip to content

Allow to generate lower case header references through the config #1310

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

Merged
merged 10 commits into from
Oct 30, 2019

Conversation

hoijui
Copy link
Contributor

@hoijui hoijui commented Oct 21, 2019

resolves #1305

This makes the references consistent/compatible with GitHub,
GitLab, Pandoc and many other tools.

This behavior can be enabled in config.json with:

"linkifyHeaderStyle": "gfm"

Signed-off-by: hoijui <hoijui.quaero@gmail.com>
…ckmdio#1305)

This makes the references consistent/compatible with GitHub,
GitLab, Pandoc and many other tools.

This behavior can be enabled in config.json with:

```
"linkifyHeaderStyle": "gfm"
```

Signed-off-by: hoijui <hoijui.quaero@gmail.com>
@Yukaii Yukaii added this to the 1.4.0 Release milestone Oct 22, 2019
@Yukaii

This comment has been minimized.

hoijui and others added 2 commits October 22, 2019 17:14
Co-Authored-By: Yukai Huang <yukaihuangtw@gmail.com>
Signed-off-by: hoijui <hoijui.quaero@gmail.com>
Co-Authored-By: Yukai Huang <yukaihuangtw@gmail.com>
Signed-off-by: hoijui <hoijui.quaero@gmail.com>
@hoijui
Copy link
Contributor Author

hoijui commented Oct 22, 2019

thanks @Yukaii , I would not have noticed ;-)

@jackycute

This comment has been minimized.

@hoijui
Copy link
Contributor Author

hoijui commented Oct 24, 2019

Just now, I actually had a look at how GitHub does it:
https://gist.github.com/asabaylus/3071099#gistcomment-1593627

The way it is done in this pull request comes close, but does not handle the "make ID unique" part.

I implemented that part locally, and it works, but .. only when I start with a blank document, and copy&paste in a sample with duplicate headers at once. If I manually start editing around, making the headers sometimes the same, sometimes not, it does not work. It looks like CodiMD handles changes on the fly, and has its own mechanism to prevent equal ids, and that interferes (and is not compatible with gfm).

any hints on where in the code this is happening?

@jackycute
Copy link
Member

any hints on where in the code this is happening?

Here you are:

codimd/public/js/extra.js

Lines 897 to 915 in e03a326

export function deduplicatedHeaderId (view) {
const headers = view.find(':header.raw').removeClass('raw').toArray()
for (let i = 0; i < headers.length; i++) {
const id = $(headers[i]).attr('id')
if (!id) continue
const duplicatedHeaders = view.find(`:header[id="${id}"]`).toArray()
for (let j = 0; j < duplicatedHeaders.length; j++) {
if (duplicatedHeaders[j] !== headers[i]) {
const newId = id + j
const $duplicatedHeader = $(duplicatedHeaders[j])
$duplicatedHeader.attr('id', newId)
const $headerLink = $duplicatedHeader.find(`> a.anchor[href="#${id}"]`)
$headerLink.attr('href', `#${newId}`)
$headerLink.attr('title', newId)
}
}
}
}

@hoijui
Copy link
Contributor Author

hoijui commented Oct 27, 2019

ok, am trying.. how can I log any debug info?

@Yukaii
Copy link
Member

Yukaii commented Oct 28, 2019

@hoijui You can insert console.log or add breakpoint in DevTools manually. Just remember do not commit those lines in VCS. 😄

@hoijui
Copy link
Contributor Author

hoijui commented Oct 28, 2019

thanks! :-)
found that out late evening yesterday aswell.. got it almost working now, except: I need a list of all headers in the document, in order. I currently fetch them with const allHeaders = view.find(:header).toArray(), which does not give them in order of appearance.
How should I fetch them?

@Yukaii
Copy link
Member

Yukaii commented Oct 28, 2019

You can use sortByDepth method like we used in building checkboxes

const lis = view.find('li.raw').removeClass('raw').sortByDepth().toArray()

@hoijui
Copy link
Contributor Author

hoijui commented Oct 28, 2019

hmm.. it gives me the same order, with and without that sort.
I do not need them sorted by header level, but by the appearance of the header in the document.

sample document:

# abc

### abc

## abc

I want them in the order they appear here, but both:

const allHeaders = view.find(`:header`).toArray()
const allHeaders = view.find(`:header`).sortByDepth().toArray()

returns them in this order:

# abc
## abc
### abc

which I guess, is sorted by (header-level/)depth.

@Yukaii
Copy link
Member

Yukaii commented Oct 28, 2019

Screen Shot 2019-10-28 at 11 48 56 PM

@hoijui I couldn't reproduce in DevTool console, could you share the whole code snippet with me? Something like where did you insert this line.

Signed-off-by: hoijui <hoijui.quaero@gmail.com>
Signed-off-by: hoijui <hoijui.quaero@gmail.com>
Signed-off-by: hoijui <hoijui.quaero@gmail.com>
@hoijui
Copy link
Contributor Author

hoijui commented Oct 29, 2019

thank you @Yukaii !
indeed, it was correct.. something else in my code went wrong.
I made 3 more commits, and it seems to work fine now, as far as my testing goes.

Signed-off-by: hoijui <hoijui.quaero@gmail.com>
@hoijui
Copy link
Contributor Author

hoijui commented Oct 29, 2019

Hi @hoijui,
Could you also add this config to https://github.com/hackmdio/codimd/blob/develop/lib/config/environment.js?
So that people can define this via environment vars.

Thanks!

did that too now

Signed-off-by: hoijui <hoijui.quaero@gmail.com>
Signed-off-by: hoijui <hoijui.quaero@gmail.com>
@Yukaii Yukaii temporarily deployed to codimd-develop-n3tdbp4fhjcj1pq October 29, 2019 14:50 Inactive
Copy link
Member

@Yukaii Yukaii left a comment

Choose a reason for hiding this comment

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

LATMLooks Awesome To Me!

@jackycute
Copy link
Member

Awesome, thanks @hoijui

@hoijui
Copy link
Contributor Author

hoijui commented Oct 29, 2019

thanks you two! @Yukaii @jackycute
you are really great at this! :-)

@Yukaii Yukaii merged commit 65ecb6d into hackmdio:develop Oct 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Git(-Hub/Lab) compatible (lower-case) header references
3 participants